Skip to contentSkip to conversation

staging: gasket: Apex uncloaks, plus fixes

PATCH New Todd Poynor 1 participant 11 messages Sep 17, 2018 Staging

lore
Share & tools
lore
#0 lore raw

cover staging: gasket: Apex uncloaks, plus fixes

From: Todd Poynor <[email protected]>

Add a description of the Gasket framework device Apex (it's a Google Edge TPU machine learning accelerator soon to be released), plus a handful of fixes.

Nick Ewalt (7):
staging: gasket: fix DMA direction for extended page tables
staging: gasket: fix data page unmap DMA direction
staging: gasket: page_table: don't unmap coherent pages
staging: gasket: fix gasket_free_coherent_memory metadata frees
staging: gasket: cleanup if dma_map_page fails in
gasket_perform_mapping
staging: gasket: page_table: use total_entries for max ext lvl0 page
idx
staging: gasket: page_table: handle failed dma_map_page

Todd Poynor (3):
staging: gasket: Kconfig: describe Apex as an Edge TPU device
staging: gasket: interrupt: remove PCI-MSIX-specific status check
staging: gasket: page table: use GFP_KERNEL for dma_alloc_coherent

 drivers/staging/gasket/Kconfig             |  5 +-
 drivers/staging/gasket/gasket_interrupt.c  |  5 --
 drivers/staging/gasket/gasket_page_table.c | 61 +++++++++++++++-------
 3 files changed, 45 insertions(+), 26 deletions(-)
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#1 lore raw

PATCH 1/10 staging: gasket: Kconfig: describe Apex as an Edge TPU device

From: Todd Poynor <[email protected]>

Add a brief description and URL for more information on the Apex device, an Edge TPU (Tensorflow Processing Unit) machine learning accelerator.

---
 drivers/staging/gasket/Kconfig | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/staging/gasket/Kconfig +3 -2
index 970e299046c37..e82b85541f7ef 100644
@@ -14,8 +14,9 @@ config STAGING_APEX_DRIVER
 	tristate "Apex Driver"
 	depends on STAGING_GASKET_FRAMEWORK
 	help
-	  This driver supports the Apex device.  Say Y if you want to
-	  include this driver in the kernel.
+	  This driver supports the Apex Edge TPU device.  See
+	  https://cloud.google.com/edge-tpu/ for more information.
+	  Say Y if you want to include this driver in the kernel.
 
 	  To compile this driver as a module, choose M here.  The module
 	  will be called "apex".
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#2 lore raw

PATCH 2/10 staging: gasket: interrupt: remove PCI-MSIX-specific status check

In reply to Todd Poynor

From: Todd Poynor <[email protected]>

Devices not using MSIX don't use the msix_initialized field, so don't require it to be set in the interrupt system status check. The general check for interrupts configured that follows can cover both MSIX and device-managed interrupts.

---
 drivers/staging/gasket/gasket_interrupt.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/staging/gasket/gasket_interrupt.c +0 -5
index 2cd262be65ca0..49d47afad64fa 100644
@@ -478,11 +478,6 @@ int gasket_interrupt_system_status(struct gasket_dev *gasket_dev)
 		return GASKET_STATUS_DEAD;
 	}
 
-	if (!gasket_dev->interrupt_data->msix_configured) {
-		dev_dbg(gasket_dev->dev, "Interrupt not initialized\n");
-		return GASKET_STATUS_LAMED;
-	}
-
 	if (gasket_dev->interrupt_data->num_configured !=
 		gasket_dev->interrupt_data->num_interrupts) {
 		dev_dbg(gasket_dev->dev,
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#3 lore raw

PATCH 3/10 staging: gasket: page table: use GFP_KERNEL for dma_alloc_coherent

In reply to Todd Poynor

From: Todd Poynor <[email protected]>

Flags should be specified for dma_alloc_coherent() call. Use GFP_KERNEL, it's fine to sleep here.

---
 drivers/staging/gasket/gasket_page_table.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gasket/gasket_page_table.c +1 -1
index 53492f4fad6aa..71b77da2e18ca 100644
@@ -1287,7 +1287,7 @@ int gasket_alloc_coherent_memory(struct gasket_dev *gasket_dev, u64 size,
 		return -EINVAL;
 
 	mem = dma_alloc_coherent(gasket_get_device(gasket_dev),
-				 num_pages * PAGE_SIZE, &handle, 0);
+				 num_pages * PAGE_SIZE, &handle, GFP_KERNEL);
 	if (!mem)
 		goto nomem;
 
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#4 lore raw

PATCH 4/10 staging: gasket: fix DMA direction for extended page tables

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

Extended page tables should be mapped as DMA_TO_DEVICE, not bi-directional.

---
 drivers/staging/gasket/gasket_page_table.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/gasket/gasket_page_table.c +2 -2
index 71b77da2e18ca..e86bdc5fc79d2 100644
@@ -320,7 +320,7 @@ static void gasket_free_extended_subtable(struct gasket_page_table *pg_tbl,
 
 	if (pte->dma_addr)
 		dma_unmap_page(pg_tbl->device, pte->dma_addr, PAGE_SIZE,
-			       DMA_BIDIRECTIONAL);
+			       DMA_TO_DEVICE);
 
 	vfree(pte->sublevel);
 
@@ -894,7 +894,7 @@ static int gasket_alloc_extended_subtable(struct gasket_page_table *pg_tbl,
 
 	/* Map the page into DMA space. */
 	pte->dma_addr = dma_map_page(pg_tbl->device, pte->page, 0, PAGE_SIZE,
-				     DMA_BIDIRECTIONAL);
+				     DMA_TO_DEVICE);
 
 	/* make the addresses available to the device */
 	dma_addr = (pte->dma_addr + pte->offset) | GASKET_VALID_SLOT_FLAG;
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#5 lore raw

PATCH 5/10 staging: gasket: fix data page unmap DMA direction

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

The DMA direction supplied to dma_unmap_page should match the corresponding dma_map_page call, which is mapped bi-directional.

---
 drivers/staging/gasket/gasket_page_table.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gasket/gasket_page_table.c +1 -1
index e86bdc5fc79d2..8fe27e7d1b53c 100644
@@ -612,7 +612,7 @@ static void gasket_perform_unmapping(struct gasket_page_table *pg_tbl,
 		if (ptes[i].status == PTE_INUSE) {
 			if (ptes[i].dma_addr) {
 				dma_unmap_page(pg_tbl->device, ptes[i].dma_addr,
-					       PAGE_SIZE, DMA_FROM_DEVICE);
+					       PAGE_SIZE, DMA_BIDIRECTIONAL);
 			}
 			if (gasket_release_page(ptes[i].page))
 				--pg_tbl->num_active_pages;
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#6 lore raw

PATCH 6/10 staging: gasket: page_table: don't unmap coherent pages

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

Only call dma_unmap_page if there was an associated dma_map_page call.

---
 drivers/staging/gasket/gasket_page_table.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gasket/gasket_page_table.c +1 -1
index 8fe27e7d1b53c..33d98043953a5 100644
@@ -610,7 +610,7 @@ static void gasket_perform_unmapping(struct gasket_page_table *pg_tbl,
 
 		/* release the address from the driver, */
 		if (ptes[i].status == PTE_INUSE) {
-			if (ptes[i].dma_addr) {
+			if (ptes[i].page && ptes[i].dma_addr) {
 				dma_unmap_page(pg_tbl->device, ptes[i].dma_addr,
 					       PAGE_SIZE, DMA_BIDIRECTIONAL);
 			}
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#7 lore raw

PATCH 7/10 staging: gasket: fix gasket_free_coherent_memory metadata frees

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

Free gasket_coherent_page_entries metadata memory, update data structures accordingly.

---
 drivers/staging/gasket/gasket_page_table.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/staging/gasket/gasket_page_table.c +5 -0
index 33d98043953a5..c1ce8f984f8e0 100644
@@ -1353,6 +1353,11 @@ int gasket_free_coherent_memory(struct gasket_dev *gasket_dev, u64 size,
 		gasket_dev->coherent_buffer.virt_base = NULL;
 		gasket_dev->coherent_buffer.phys_base = 0;
 	}
+
+	kfree(gasket_dev->page_table[index]->coherent_pages);
+	gasket_dev->page_table[index]->coherent_pages = NULL;
+	gasket_dev->page_table[index]->num_coherent_pages = 0;
+
 	return 0;
 }
 
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#8 lore raw

PATCH 8/10 staging: gasket: cleanup if dma_map_page fails in gasket_perform_mapping

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

Previously pages would have never been unmapped in this case.

---
 drivers/staging/gasket/gasket_page_table.c | 33 +++++++++++++---------
 1 file changed, 20 insertions(+), 13 deletions(-)
drivers/staging/gasket/gasket_page_table.c +20 -13
index c1ce8f984f8e0..779ad2f23ef9b 100644
@@ -433,6 +433,19 @@ static int is_coherent(struct gasket_page_table *pg_tbl, ulong host_addr)
 	return min <= host_addr && host_addr < max;
 }
 
+/* Safely return a page to the OS. */
+static bool gasket_release_page(struct page *page)
+{
+	if (!page)
+		return false;
+
+	if (!PageReserved(page))
+		SetPageDirty(page);
+	put_page(page);
+
+	return true;
+}
+
 /*
  * Get and map last level page table buffers.
  *
@@ -500,6 +513,13 @@ static int gasket_perform_mapping(struct gasket_page_table *pg_tbl,
 					(unsigned long long)ptes[i].dma_addr,
 					(void *)page_to_pfn(page),
 					(void *)page_to_phys(page));
+
+				/* clean up */
+				if (gasket_release_page(ptes[i].page))
+					--pg_tbl->num_active_pages;
+
+				memset(&ptes[i], 0,
+				       sizeof(struct gasket_page_table_entry));
 				return -1;
 			}
 		}
@@ -571,19 +591,6 @@ static int gasket_alloc_simple_entries(struct gasket_page_table *pg_tbl,
 	return 0;
 }
 
-/* Safely return a page to the OS. */
-static bool gasket_release_page(struct page *page)
-{
-	if (!page)
-		return false;
-
-	if (!PageReserved(page))
-		SetPageDirty(page);
-	put_page(page);
-
-	return true;
-}
-
 /*
  * Unmap and release mapped pages.
  * The page table mutex must be held by the caller.
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#9 lore raw

PATCH 9/10 staging: gasket: page_table: use total_entries for max ext lvl0 page idx

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

The maximum number of entries in the page table is configurable at initialization time and should be used in gasket_extended_lvl0_page_idx.

---
 drivers/staging/gasket/gasket_page_table.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/gasket/gasket_page_table.c +1 -1
index 779ad2f23ef9b..8364b49f147c2 100644
@@ -562,7 +562,7 @@ static ulong gasket_extended_lvl0_page_idx(struct gasket_page_table *pg_tbl,
 					   ulong dev_addr)
 {
 	return (dev_addr >> GASKET_EXTENDED_LVL0_SHIFT) &
-	       ((1 << GASKET_EXTENDED_LVL0_WIDTH) - 1);
+		(pg_tbl->config.total_entries - 1);
 }
 
 /*
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#10 lore raw

PATCH 10/10 staging: gasket: page_table: handle failed dma_map_page

In reply to Todd Poynor

From: Nick Ewalt <[email protected]>

Handle dma_map_page failing in gasket_alloc_extended_subtable: free memory, don't add invalid page table entry.

---
 drivers/staging/gasket/gasket_page_table.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/staging/gasket/gasket_page_table.c +11 -0
index 8364b49f147c2..964146f0df526 100644
@@ -902,6 +902,17 @@ static int gasket_alloc_extended_subtable(struct gasket_page_table *pg_tbl,
 	/* Map the page into DMA space. */
 	pte->dma_addr = dma_map_page(pg_tbl->device, pte->page, 0, PAGE_SIZE,
 				     DMA_TO_DEVICE);
+	if (dma_mapping_error(pg_tbl->device, pte->dma_addr)) {
+		dev_dbg(pg_tbl->device,
+			"%s: fail to map page [pfn %lx phys %llx]\n",
+			__func__, page_to_pfn(pte->page),
+			page_to_phys(pte->page));
+
+		free_page(page_addr);
+		vfree(pte->sublevel);
+		memset(pte, 0, sizeof(struct gasket_page_table_entry));
+		return -ENOMEM;
+	}
 
 	/* make the addresses available to the device */
 	dma_addr = (pte->dma_addr + pte->offset) | GASKET_VALID_SLOT_FLAG;
Signature · 1 line
2.19.0.397.gdd90340f6a-goog