99#include <linux/log2.h>
1010#include <linux/seq_file.h>
1111#include <linux/slab.h>
12+ #include <linux/uacce.h>
13+ #include <linux/uaccess.h>
14+ #include <uapi/misc/uacce/hisi_qm.h>
1215#include "qm.h"
1316
1417/* eq/aeq irq enable */
@@ -466,9 +469,14 @@ static void qm_cq_head_update(struct hisi_qp *qp)
466469
467470static void qm_poll_qp (struct hisi_qp * qp , struct hisi_qm * qm )
468471{
469- struct qm_cqe * cqe = qp -> cqe + qp -> qp_status .cq_head ;
472+ if (qp -> event_cb ) {
473+ qp -> event_cb (qp );
474+ return ;
475+ }
470476
471477 if (qp -> req_cb ) {
478+ struct qm_cqe * cqe = qp -> cqe + qp -> qp_status .cq_head ;
479+
472480 while (QM_CQE_PHASE (cqe ) == qp -> qp_status .cqc_phase ) {
473481 dma_rmb ();
474482 qp -> req_cb (qp , qp -> sqe + qm -> sqe_size *
@@ -1273,7 +1281,7 @@ static int qm_qp_ctx_cfg(struct hisi_qp *qp, int qp_id, int pasid)
12731281 * @qp: The qp we want to start to run.
12741282 * @arg: Accelerator specific argument.
12751283 *
1276- * After this function, qp can receive request from user. Return qp_id if
1284+ * After this function, qp can receive request from user. Return 0 if
12771285 * successful, Return -EBUSY if failed.
12781286 */
12791287int hisi_qm_start_qp (struct hisi_qp * qp , unsigned long arg )
@@ -1318,7 +1326,7 @@ int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg)
13181326
13191327 dev_dbg (dev , "queue %d started\n" , qp_id );
13201328
1321- return qp_id ;
1329+ return 0 ;
13221330}
13231331EXPORT_SYMBOL_GPL (hisi_qm_start_qp );
13241332
@@ -1399,6 +1407,214 @@ static void hisi_qm_cache_wb(struct hisi_qm *qm)
13991407 }
14001408}
14011409
1410+ static void qm_qp_event_notifier (struct hisi_qp * qp )
1411+ {
1412+ wake_up_interruptible (& qp -> uacce_q -> wait );
1413+ }
1414+
1415+ static int hisi_qm_get_available_instances (struct uacce_device * uacce )
1416+ {
1417+ int i , ret ;
1418+ struct hisi_qm * qm = uacce -> priv ;
1419+
1420+ read_lock (& qm -> qps_lock );
1421+ for (i = 0 , ret = 0 ; i < qm -> qp_num ; i ++ )
1422+ if (!qm -> qp_array [i ])
1423+ ret ++ ;
1424+ read_unlock (& qm -> qps_lock );
1425+
1426+ return ret ;
1427+ }
1428+
1429+ static int hisi_qm_uacce_get_queue (struct uacce_device * uacce ,
1430+ unsigned long arg ,
1431+ struct uacce_queue * q )
1432+ {
1433+ struct hisi_qm * qm = uacce -> priv ;
1434+ struct hisi_qp * qp ;
1435+ u8 alg_type = 0 ;
1436+
1437+ qp = hisi_qm_create_qp (qm , alg_type );
1438+ if (IS_ERR (qp ))
1439+ return PTR_ERR (qp );
1440+
1441+ q -> priv = qp ;
1442+ q -> uacce = uacce ;
1443+ qp -> uacce_q = q ;
1444+ qp -> event_cb = qm_qp_event_notifier ;
1445+ qp -> pasid = arg ;
1446+
1447+ return 0 ;
1448+ }
1449+
1450+ static void hisi_qm_uacce_put_queue (struct uacce_queue * q )
1451+ {
1452+ struct hisi_qp * qp = q -> priv ;
1453+
1454+ hisi_qm_cache_wb (qp -> qm );
1455+ hisi_qm_release_qp (qp );
1456+ }
1457+
1458+ /* map sq/cq/doorbell to user space */
1459+ static int hisi_qm_uacce_mmap (struct uacce_queue * q ,
1460+ struct vm_area_struct * vma ,
1461+ struct uacce_qfile_region * qfr )
1462+ {
1463+ struct hisi_qp * qp = q -> priv ;
1464+ struct hisi_qm * qm = qp -> qm ;
1465+ size_t sz = vma -> vm_end - vma -> vm_start ;
1466+ struct pci_dev * pdev = qm -> pdev ;
1467+ struct device * dev = & pdev -> dev ;
1468+ unsigned long vm_pgoff ;
1469+ int ret ;
1470+
1471+ switch (qfr -> type ) {
1472+ case UACCE_QFRT_MMIO :
1473+ if (qm -> ver == QM_HW_V2 ) {
1474+ if (sz > PAGE_SIZE * (QM_DOORBELL_PAGE_NR +
1475+ QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE ))
1476+ return - EINVAL ;
1477+ } else {
1478+ if (sz > PAGE_SIZE * QM_DOORBELL_PAGE_NR )
1479+ return - EINVAL ;
1480+ }
1481+
1482+ vma -> vm_flags |= VM_IO ;
1483+
1484+ return remap_pfn_range (vma , vma -> vm_start ,
1485+ qm -> phys_base >> PAGE_SHIFT ,
1486+ sz , pgprot_noncached (vma -> vm_page_prot ));
1487+ case UACCE_QFRT_DUS :
1488+ if (sz != qp -> qdma .size )
1489+ return - EINVAL ;
1490+
1491+ /*
1492+ * dma_mmap_coherent() requires vm_pgoff as 0
1493+ * restore vm_pfoff to initial value for mmap()
1494+ */
1495+ vm_pgoff = vma -> vm_pgoff ;
1496+ vma -> vm_pgoff = 0 ;
1497+ ret = dma_mmap_coherent (dev , vma , qp -> qdma .va ,
1498+ qp -> qdma .dma , sz );
1499+ vma -> vm_pgoff = vm_pgoff ;
1500+ return ret ;
1501+
1502+ default :
1503+ return - EINVAL ;
1504+ }
1505+ }
1506+
1507+ static int hisi_qm_uacce_start_queue (struct uacce_queue * q )
1508+ {
1509+ struct hisi_qp * qp = q -> priv ;
1510+
1511+ return hisi_qm_start_qp (qp , qp -> pasid );
1512+ }
1513+
1514+ static void hisi_qm_uacce_stop_queue (struct uacce_queue * q )
1515+ {
1516+ hisi_qm_stop_qp (q -> priv );
1517+ }
1518+
1519+ static int qm_set_sqctype (struct uacce_queue * q , u16 type )
1520+ {
1521+ struct hisi_qm * qm = q -> uacce -> priv ;
1522+ struct hisi_qp * qp = q -> priv ;
1523+
1524+ write_lock (& qm -> qps_lock );
1525+ qp -> alg_type = type ;
1526+ write_unlock (& qm -> qps_lock );
1527+
1528+ return 0 ;
1529+ }
1530+
1531+ static long hisi_qm_uacce_ioctl (struct uacce_queue * q , unsigned int cmd ,
1532+ unsigned long arg )
1533+ {
1534+ struct hisi_qp * qp = q -> priv ;
1535+ struct hisi_qp_ctx qp_ctx ;
1536+
1537+ if (cmd == UACCE_CMD_QM_SET_QP_CTX ) {
1538+ if (copy_from_user (& qp_ctx , (void __user * )arg ,
1539+ sizeof (struct hisi_qp_ctx )))
1540+ return - EFAULT ;
1541+
1542+ if (qp_ctx .qc_type != 0 && qp_ctx .qc_type != 1 )
1543+ return - EINVAL ;
1544+
1545+ qm_set_sqctype (q , qp_ctx .qc_type );
1546+ qp_ctx .id = qp -> qp_id ;
1547+
1548+ if (copy_to_user ((void __user * )arg , & qp_ctx ,
1549+ sizeof (struct hisi_qp_ctx )))
1550+ return - EFAULT ;
1551+ } else {
1552+ return - EINVAL ;
1553+ }
1554+
1555+ return 0 ;
1556+ }
1557+
1558+ static const struct uacce_ops uacce_qm_ops = {
1559+ .get_available_instances = hisi_qm_get_available_instances ,
1560+ .get_queue = hisi_qm_uacce_get_queue ,
1561+ .put_queue = hisi_qm_uacce_put_queue ,
1562+ .start_queue = hisi_qm_uacce_start_queue ,
1563+ .stop_queue = hisi_qm_uacce_stop_queue ,
1564+ .mmap = hisi_qm_uacce_mmap ,
1565+ .ioctl = hisi_qm_uacce_ioctl ,
1566+ };
1567+
1568+ static int qm_alloc_uacce (struct hisi_qm * qm )
1569+ {
1570+ struct pci_dev * pdev = qm -> pdev ;
1571+ struct uacce_device * uacce ;
1572+ unsigned long mmio_page_nr ;
1573+ unsigned long dus_page_nr ;
1574+ struct uacce_interface interface = {
1575+ .flags = UACCE_DEV_SVA ,
1576+ .ops = & uacce_qm_ops ,
1577+ };
1578+
1579+ strncpy (interface .name , pdev -> driver -> name , sizeof (interface .name ));
1580+
1581+ uacce = uacce_alloc (& pdev -> dev , & interface );
1582+ if (IS_ERR (uacce ))
1583+ return PTR_ERR (uacce );
1584+
1585+ if (uacce -> flags & UACCE_DEV_SVA ) {
1586+ qm -> use_sva = true;
1587+ } else {
1588+ /* only consider sva case */
1589+ uacce_remove (uacce );
1590+ qm -> uacce = NULL ;
1591+ return - EINVAL ;
1592+ }
1593+
1594+ uacce -> is_vf = pdev -> is_virtfn ;
1595+ uacce -> priv = qm ;
1596+ uacce -> algs = qm -> algs ;
1597+
1598+ if (qm -> ver == QM_HW_V1 ) {
1599+ mmio_page_nr = QM_DOORBELL_PAGE_NR ;
1600+ uacce -> api_ver = HISI_QM_API_VER_BASE ;
1601+ } else {
1602+ mmio_page_nr = QM_DOORBELL_PAGE_NR +
1603+ QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE ;
1604+ uacce -> api_ver = HISI_QM_API_VER2_BASE ;
1605+ }
1606+
1607+ dus_page_nr = (PAGE_SIZE - 1 + qm -> sqe_size * QM_Q_DEPTH +
1608+ sizeof (struct qm_cqe ) * QM_Q_DEPTH ) >> PAGE_SHIFT ;
1609+
1610+ uacce -> qf_pg_num [UACCE_QFRT_MMIO ] = mmio_page_nr ;
1611+ uacce -> qf_pg_num [UACCE_QFRT_DUS ] = dus_page_nr ;
1612+
1613+ qm -> uacce = uacce ;
1614+
1615+ return 0 ;
1616+ }
1617+
14021618/**
14031619 * hisi_qm_get_free_qp_num() - Get free number of qp in qm.
14041620 * @qm: The qm which want to get free qp.
@@ -1441,10 +1657,14 @@ int hisi_qm_init(struct hisi_qm *qm)
14411657 return - EINVAL ;
14421658 }
14431659
1660+ ret = qm_alloc_uacce (qm );
1661+ if (ret < 0 )
1662+ dev_warn (& pdev -> dev , "fail to alloc uacce (%d)\n" , ret );
1663+
14441664 ret = pci_enable_device_mem (pdev );
14451665 if (ret < 0 ) {
14461666 dev_err (& pdev -> dev , "Failed to enable device mem!\n" );
1447- return ret ;
1667+ goto err_remove_uacce ;
14481668 }
14491669
14501670 ret = pci_request_mem_regions (pdev , qm -> dev_name );
@@ -1453,8 +1673,9 @@ int hisi_qm_init(struct hisi_qm *qm)
14531673 goto err_disable_pcidev ;
14541674 }
14551675
1456- qm -> io_base = ioremap (pci_resource_start (pdev , PCI_BAR_2 ),
1457- pci_resource_len (qm -> pdev , PCI_BAR_2 ));
1676+ qm -> phys_base = pci_resource_start (pdev , PCI_BAR_2 );
1677+ qm -> phys_size = pci_resource_len (qm -> pdev , PCI_BAR_2 );
1678+ qm -> io_base = ioremap (qm -> phys_base , qm -> phys_size );
14581679 if (!qm -> io_base ) {
14591680 ret = - EIO ;
14601681 goto err_release_mem_regions ;
@@ -1497,6 +1718,9 @@ int hisi_qm_init(struct hisi_qm *qm)
14971718 pci_release_mem_regions (pdev );
14981719err_disable_pcidev :
14991720 pci_disable_device (pdev );
1721+ err_remove_uacce :
1722+ uacce_remove (qm -> uacce );
1723+ qm -> uacce = NULL ;
15001724
15011725 return ret ;
15021726}
@@ -1513,6 +1737,9 @@ void hisi_qm_uninit(struct hisi_qm *qm)
15131737 struct pci_dev * pdev = qm -> pdev ;
15141738 struct device * dev = & pdev -> dev ;
15151739
1740+ uacce_remove (qm -> uacce );
1741+ qm -> uacce = NULL ;
1742+
15161743 if (qm -> use_dma_api && qm -> qdma .va ) {
15171744 hisi_qm_cache_wb (qm );
15181745 dma_free_coherent (dev , qm -> qdma .size ,
0 commit comments