gk20a.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. * GK20A does not have dedicated video memory, and to accurately represent this
  24. * fact Nouveau will not create a RAM device for it. Therefore its instmem
  25. * implementation must be done directly on top of system memory, while
  26. * preserving coherency for read and write operations.
  27. *
  28. * Instmem can be allocated through two means:
  29. * 1) If an IOMMU unit has been probed, the IOMMU API is used to make memory
  30. * pages contiguous to the GPU. This is the preferred way.
  31. * 2) If no IOMMU unit is probed, the DMA API is used to allocate physically
  32. * contiguous memory.
  33. *
  34. * In both cases CPU read and writes are performed by creating a write-combined
  35. * mapping. The GPU L2 cache must thus be flushed/invalidated when required. To
  36. * be conservative we do this every time we acquire or release an instobj, but
  37. * ideally L2 management should be handled at a higher level.
  38. *
  39. * To improve performance, CPU mappings are not removed upon instobj release.
  40. * Instead they are placed into a LRU list to be recycled when the mapped space
  41. * goes beyond a certain threshold. At the moment this limit is 1MB.
  42. */
  43. #include "priv.h"
  44. #include <core/memory.h>
  45. #include <core/mm.h>
  46. #include <core/tegra.h>
  47. #include <subdev/fb.h>
  48. #include <subdev/ltc.h>
  49. struct gk20a_instobj {
  50. struct nvkm_memory memory;
  51. struct nvkm_mem mem;
  52. struct gk20a_instmem *imem;
  53. /* CPU mapping */
  54. u32 *vaddr;
  55. };
  56. #define gk20a_instobj(p) container_of((p), struct gk20a_instobj, memory)
  57. /*
  58. * Used for objects allocated using the DMA API
  59. */
  60. struct gk20a_instobj_dma {
  61. struct gk20a_instobj base;
  62. dma_addr_t handle;
  63. struct nvkm_mm_node r;
  64. };
  65. #define gk20a_instobj_dma(p) \
  66. container_of(gk20a_instobj(p), struct gk20a_instobj_dma, base)
  67. /*
  68. * Used for objects flattened using the IOMMU API
  69. */
  70. struct gk20a_instobj_iommu {
  71. struct gk20a_instobj base;
  72. /* to link into gk20a_instmem::vaddr_lru */
  73. struct list_head vaddr_node;
  74. /* how many clients are using vaddr? */
  75. u32 use_cpt;
  76. /* will point to the higher half of pages */
  77. dma_addr_t *dma_addrs;
  78. /* array of base.mem->size pages (+ dma_addr_ts) */
  79. struct page *pages[];
  80. };
  81. #define gk20a_instobj_iommu(p) \
  82. container_of(gk20a_instobj(p), struct gk20a_instobj_iommu, base)
  83. struct gk20a_instmem {
  84. struct nvkm_instmem base;
  85. /* protects vaddr_* and gk20a_instobj::vaddr* */
  86. spinlock_t lock;
  87. /* CPU mappings LRU */
  88. unsigned int vaddr_use;
  89. unsigned int vaddr_max;
  90. struct list_head vaddr_lru;
  91. /* Only used if IOMMU if present */
  92. struct mutex *mm_mutex;
  93. struct nvkm_mm *mm;
  94. struct iommu_domain *domain;
  95. unsigned long iommu_pgshift;
  96. u16 iommu_bit;
  97. /* Only used by DMA API */
  98. unsigned long attrs;
  99. };
  100. #define gk20a_instmem(p) container_of((p), struct gk20a_instmem, base)
  101. static enum nvkm_memory_target
  102. gk20a_instobj_target(struct nvkm_memory *memory)
  103. {
  104. return NVKM_MEM_TARGET_NCOH;
  105. }
  106. static u64
  107. gk20a_instobj_addr(struct nvkm_memory *memory)
  108. {
  109. return gk20a_instobj(memory)->mem.offset;
  110. }
  111. static u64
  112. gk20a_instobj_size(struct nvkm_memory *memory)
  113. {
  114. return (u64)gk20a_instobj(memory)->mem.size << 12;
  115. }
  116. /*
  117. * Recycle the vaddr of obj. Must be called with gk20a_instmem::lock held.
  118. */
  119. static void
  120. gk20a_instobj_iommu_recycle_vaddr(struct gk20a_instobj_iommu *obj)
  121. {
  122. struct gk20a_instmem *imem = obj->base.imem;
  123. /* there should not be any user left... */
  124. WARN_ON(obj->use_cpt);
  125. list_del(&obj->vaddr_node);
  126. vunmap(obj->base.vaddr);
  127. obj->base.vaddr = NULL;
  128. imem->vaddr_use -= nvkm_memory_size(&obj->base.memory);
  129. nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n", imem->vaddr_use,
  130. imem->vaddr_max);
  131. }
  132. /*
  133. * Must be called while holding gk20a_instmem::lock
  134. */
  135. static void
  136. gk20a_instmem_vaddr_gc(struct gk20a_instmem *imem, const u64 size)
  137. {
  138. while (imem->vaddr_use + size > imem->vaddr_max) {
  139. /* no candidate that can be unmapped, abort... */
  140. if (list_empty(&imem->vaddr_lru))
  141. break;
  142. gk20a_instobj_iommu_recycle_vaddr(
  143. list_first_entry(&imem->vaddr_lru,
  144. struct gk20a_instobj_iommu, vaddr_node));
  145. }
  146. }
  147. static void __iomem *
  148. gk20a_instobj_acquire_dma(struct nvkm_memory *memory)
  149. {
  150. struct gk20a_instobj *node = gk20a_instobj(memory);
  151. struct gk20a_instmem *imem = node->imem;
  152. struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
  153. nvkm_ltc_flush(ltc);
  154. return node->vaddr;
  155. }
  156. static void __iomem *
  157. gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
  158. {
  159. struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
  160. struct gk20a_instmem *imem = node->base.imem;
  161. struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
  162. const u64 size = nvkm_memory_size(memory);
  163. unsigned long flags;
  164. nvkm_ltc_flush(ltc);
  165. spin_lock_irqsave(&imem->lock, flags);
  166. if (node->base.vaddr) {
  167. if (!node->use_cpt) {
  168. /* remove from LRU list since mapping in use again */
  169. list_del(&node->vaddr_node);
  170. }
  171. goto out;
  172. }
  173. /* try to free some address space if we reached the limit */
  174. gk20a_instmem_vaddr_gc(imem, size);
  175. /* map the pages */
  176. node->base.vaddr = vmap(node->pages, size >> PAGE_SHIFT, VM_MAP,
  177. pgprot_writecombine(PAGE_KERNEL));
  178. if (!node->base.vaddr) {
  179. nvkm_error(&imem->base.subdev, "cannot map instobj - "
  180. "this is not going to end well...\n");
  181. goto out;
  182. }
  183. imem->vaddr_use += size;
  184. nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n",
  185. imem->vaddr_use, imem->vaddr_max);
  186. out:
  187. node->use_cpt++;
  188. spin_unlock_irqrestore(&imem->lock, flags);
  189. return node->base.vaddr;
  190. }
  191. static void
  192. gk20a_instobj_release_dma(struct nvkm_memory *memory)
  193. {
  194. struct gk20a_instobj *node = gk20a_instobj(memory);
  195. struct gk20a_instmem *imem = node->imem;
  196. struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
  197. /* in case we got a write-combined mapping */
  198. wmb();
  199. nvkm_ltc_invalidate(ltc);
  200. }
  201. static void
  202. gk20a_instobj_release_iommu(struct nvkm_memory *memory)
  203. {
  204. struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
  205. struct gk20a_instmem *imem = node->base.imem;
  206. struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
  207. unsigned long flags;
  208. spin_lock_irqsave(&imem->lock, flags);
  209. /* we should at least have one user to release... */
  210. if (WARN_ON(node->use_cpt == 0))
  211. goto out;
  212. /* add unused objs to the LRU list to recycle their mapping */
  213. if (--node->use_cpt == 0)
  214. list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
  215. out:
  216. spin_unlock_irqrestore(&imem->lock, flags);
  217. wmb();
  218. nvkm_ltc_invalidate(ltc);
  219. }
  220. static u32
  221. gk20a_instobj_rd32(struct nvkm_memory *memory, u64 offset)
  222. {
  223. struct gk20a_instobj *node = gk20a_instobj(memory);
  224. return node->vaddr[offset / 4];
  225. }
  226. static void
  227. gk20a_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
  228. {
  229. struct gk20a_instobj *node = gk20a_instobj(memory);
  230. node->vaddr[offset / 4] = data;
  231. }
  232. static void
  233. gk20a_instobj_map(struct nvkm_memory *memory, struct nvkm_vma *vma, u64 offset)
  234. {
  235. struct gk20a_instobj *node = gk20a_instobj(memory);
  236. nvkm_vm_map_at(vma, offset, &node->mem);
  237. }
  238. static void *
  239. gk20a_instobj_dtor_dma(struct nvkm_memory *memory)
  240. {
  241. struct gk20a_instobj_dma *node = gk20a_instobj_dma(memory);
  242. struct gk20a_instmem *imem = node->base.imem;
  243. struct device *dev = imem->base.subdev.device->dev;
  244. if (unlikely(!node->base.vaddr))
  245. goto out;
  246. dma_free_attrs(dev, node->base.mem.size << PAGE_SHIFT, node->base.vaddr,
  247. node->handle, imem->attrs);
  248. out:
  249. return node;
  250. }
  251. static void *
  252. gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
  253. {
  254. struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
  255. struct gk20a_instmem *imem = node->base.imem;
  256. struct device *dev = imem->base.subdev.device->dev;
  257. struct nvkm_mm_node *r = node->base.mem.mem;
  258. unsigned long flags;
  259. int i;
  260. if (unlikely(!r))
  261. goto out;
  262. spin_lock_irqsave(&imem->lock, flags);
  263. /* vaddr has already been recycled */
  264. if (node->base.vaddr)
  265. gk20a_instobj_iommu_recycle_vaddr(node);
  266. spin_unlock_irqrestore(&imem->lock, flags);
  267. /* clear IOMMU bit to unmap pages */
  268. r->offset &= ~BIT(imem->iommu_bit - imem->iommu_pgshift);
  269. /* Unmap pages from GPU address space and free them */
  270. for (i = 0; i < node->base.mem.size; i++) {
  271. iommu_unmap(imem->domain,
  272. (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
  273. dma_unmap_page(dev, node->dma_addrs[i], PAGE_SIZE,
  274. DMA_BIDIRECTIONAL);
  275. __free_page(node->pages[i]);
  276. }
  277. /* Release area from GPU address space */
  278. mutex_lock(imem->mm_mutex);
  279. nvkm_mm_free(imem->mm, &r);
  280. mutex_unlock(imem->mm_mutex);
  281. out:
  282. return node;
  283. }
  284. static const struct nvkm_memory_func
  285. gk20a_instobj_func_dma = {
  286. .dtor = gk20a_instobj_dtor_dma,
  287. .target = gk20a_instobj_target,
  288. .addr = gk20a_instobj_addr,
  289. .size = gk20a_instobj_size,
  290. .acquire = gk20a_instobj_acquire_dma,
  291. .release = gk20a_instobj_release_dma,
  292. .rd32 = gk20a_instobj_rd32,
  293. .wr32 = gk20a_instobj_wr32,
  294. .map = gk20a_instobj_map,
  295. };
  296. static const struct nvkm_memory_func
  297. gk20a_instobj_func_iommu = {
  298. .dtor = gk20a_instobj_dtor_iommu,
  299. .target = gk20a_instobj_target,
  300. .addr = gk20a_instobj_addr,
  301. .size = gk20a_instobj_size,
  302. .acquire = gk20a_instobj_acquire_iommu,
  303. .release = gk20a_instobj_release_iommu,
  304. .rd32 = gk20a_instobj_rd32,
  305. .wr32 = gk20a_instobj_wr32,
  306. .map = gk20a_instobj_map,
  307. };
  308. static int
  309. gk20a_instobj_ctor_dma(struct gk20a_instmem *imem, u32 npages, u32 align,
  310. struct gk20a_instobj **_node)
  311. {
  312. struct gk20a_instobj_dma *node;
  313. struct nvkm_subdev *subdev = &imem->base.subdev;
  314. struct device *dev = subdev->device->dev;
  315. if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
  316. return -ENOMEM;
  317. *_node = &node->base;
  318. nvkm_memory_ctor(&gk20a_instobj_func_dma, &node->base.memory);
  319. node->base.vaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
  320. &node->handle, GFP_KERNEL,
  321. imem->attrs);
  322. if (!node->base.vaddr) {
  323. nvkm_error(subdev, "cannot allocate DMA memory\n");
  324. return -ENOMEM;
  325. }
  326. /* alignment check */
  327. if (unlikely(node->handle & (align - 1)))
  328. nvkm_warn(subdev,
  329. "memory not aligned as requested: %pad (0x%x)\n",
  330. &node->handle, align);
  331. /* present memory for being mapped using small pages */
  332. node->r.type = 12;
  333. node->r.offset = node->handle >> 12;
  334. node->r.length = (npages << PAGE_SHIFT) >> 12;
  335. node->base.mem.offset = node->handle;
  336. node->base.mem.mem = &node->r;
  337. return 0;
  338. }
  339. static int
  340. gk20a_instobj_ctor_iommu(struct gk20a_instmem *imem, u32 npages, u32 align,
  341. struct gk20a_instobj **_node)
  342. {
  343. struct gk20a_instobj_iommu *node;
  344. struct nvkm_subdev *subdev = &imem->base.subdev;
  345. struct device *dev = subdev->device->dev;
  346. struct nvkm_mm_node *r;
  347. int ret;
  348. int i;
  349. /*
  350. * despite their variable size, instmem allocations are small enough
  351. * (< 1 page) to be handled by kzalloc
  352. */
  353. if (!(node = kzalloc(sizeof(*node) + ((sizeof(node->pages[0]) +
  354. sizeof(*node->dma_addrs)) * npages), GFP_KERNEL)))
  355. return -ENOMEM;
  356. *_node = &node->base;
  357. node->dma_addrs = (void *)(node->pages + npages);
  358. nvkm_memory_ctor(&gk20a_instobj_func_iommu, &node->base.memory);
  359. /* Allocate backing memory */
  360. for (i = 0; i < npages; i++) {
  361. struct page *p = alloc_page(GFP_KERNEL);
  362. dma_addr_t dma_adr;
  363. if (p == NULL) {
  364. ret = -ENOMEM;
  365. goto free_pages;
  366. }
  367. node->pages[i] = p;
  368. dma_adr = dma_map_page(dev, p, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
  369. if (dma_mapping_error(dev, dma_adr)) {
  370. nvkm_error(subdev, "DMA mapping error!\n");
  371. ret = -ENOMEM;
  372. goto free_pages;
  373. }
  374. node->dma_addrs[i] = dma_adr;
  375. }
  376. mutex_lock(imem->mm_mutex);
  377. /* Reserve area from GPU address space */
  378. ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
  379. align >> imem->iommu_pgshift, &r);
  380. mutex_unlock(imem->mm_mutex);
  381. if (ret) {
  382. nvkm_error(subdev, "IOMMU space is full!\n");
  383. goto free_pages;
  384. }
  385. /* Map into GPU address space */
  386. for (i = 0; i < npages; i++) {
  387. u32 offset = (r->offset + i) << imem->iommu_pgshift;
  388. ret = iommu_map(imem->domain, offset, node->dma_addrs[i],
  389. PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
  390. if (ret < 0) {
  391. nvkm_error(subdev, "IOMMU mapping failure: %d\n", ret);
  392. while (i-- > 0) {
  393. offset -= PAGE_SIZE;
  394. iommu_unmap(imem->domain, offset, PAGE_SIZE);
  395. }
  396. goto release_area;
  397. }
  398. }
  399. /* IOMMU bit tells that an address is to be resolved through the IOMMU */
  400. r->offset |= BIT(imem->iommu_bit - imem->iommu_pgshift);
  401. node->base.mem.offset = ((u64)r->offset) << imem->iommu_pgshift;
  402. node->base.mem.mem = r;
  403. return 0;
  404. release_area:
  405. mutex_lock(imem->mm_mutex);
  406. nvkm_mm_free(imem->mm, &r);
  407. mutex_unlock(imem->mm_mutex);
  408. free_pages:
  409. for (i = 0; i < npages && node->pages[i] != NULL; i++) {
  410. dma_addr_t dma_addr = node->dma_addrs[i];
  411. if (dma_addr)
  412. dma_unmap_page(dev, dma_addr, PAGE_SIZE,
  413. DMA_BIDIRECTIONAL);
  414. __free_page(node->pages[i]);
  415. }
  416. return ret;
  417. }
  418. static int
  419. gk20a_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
  420. struct nvkm_memory **pmemory)
  421. {
  422. struct gk20a_instmem *imem = gk20a_instmem(base);
  423. struct nvkm_subdev *subdev = &imem->base.subdev;
  424. struct gk20a_instobj *node = NULL;
  425. int ret;
  426. nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
  427. imem->domain ? "IOMMU" : "DMA", size, align);
  428. /* Round size and align to page bounds */
  429. size = max(roundup(size, PAGE_SIZE), PAGE_SIZE);
  430. align = max(roundup(align, PAGE_SIZE), PAGE_SIZE);
  431. if (imem->domain)
  432. ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT,
  433. align, &node);
  434. else
  435. ret = gk20a_instobj_ctor_dma(imem, size >> PAGE_SHIFT,
  436. align, &node);
  437. *pmemory = node ? &node->memory : NULL;
  438. if (ret)
  439. return ret;
  440. node->imem = imem;
  441. /* present memory for being mapped using small pages */
  442. node->mem.size = size >> 12;
  443. node->mem.memtype = 0;
  444. node->mem.page_shift = 12;
  445. nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
  446. size, align, node->mem.offset);
  447. return 0;
  448. }
  449. static void *
  450. gk20a_instmem_dtor(struct nvkm_instmem *base)
  451. {
  452. struct gk20a_instmem *imem = gk20a_instmem(base);
  453. /* perform some sanity checks... */
  454. if (!list_empty(&imem->vaddr_lru))
  455. nvkm_warn(&base->subdev, "instobj LRU not empty!\n");
  456. if (imem->vaddr_use != 0)
  457. nvkm_warn(&base->subdev, "instobj vmap area not empty! "
  458. "0x%x bytes still mapped\n", imem->vaddr_use);
  459. return imem;
  460. }
  461. static const struct nvkm_instmem_func
  462. gk20a_instmem = {
  463. .dtor = gk20a_instmem_dtor,
  464. .memory_new = gk20a_instobj_new,
  465. .persistent = true,
  466. .zero = false,
  467. };
  468. int
  469. gk20a_instmem_new(struct nvkm_device *device, int index,
  470. struct nvkm_instmem **pimem)
  471. {
  472. struct nvkm_device_tegra *tdev = device->func->tegra(device);
  473. struct gk20a_instmem *imem;
  474. if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
  475. return -ENOMEM;
  476. nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
  477. spin_lock_init(&imem->lock);
  478. *pimem = &imem->base;
  479. /* do not allow more than 1MB of CPU-mapped instmem */
  480. imem->vaddr_use = 0;
  481. imem->vaddr_max = 0x100000;
  482. INIT_LIST_HEAD(&imem->vaddr_lru);
  483. if (tdev->iommu.domain) {
  484. imem->mm_mutex = &tdev->iommu.mutex;
  485. imem->mm = &tdev->iommu.mm;
  486. imem->domain = tdev->iommu.domain;
  487. imem->iommu_pgshift = tdev->iommu.pgshift;
  488. imem->iommu_bit = tdev->func->iommu_bit;
  489. nvkm_info(&imem->base.subdev, "using IOMMU\n");
  490. } else {
  491. imem->attrs = DMA_ATTR_NON_CONSISTENT |
  492. DMA_ATTR_WEAK_ORDERING |
  493. DMA_ATTR_WRITE_COMBINE;
  494. nvkm_info(&imem->base.subdev, "using DMA API\n");
  495. }
  496. return 0;
  497. }