gk20a.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. struct mutex 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. nvkm_ltc_flush(ltc);
  164. mutex_lock(&imem->lock);
  165. if (node->base.vaddr) {
  166. if (!node->use_cpt) {
  167. /* remove from LRU list since mapping in use again */
  168. list_del(&node->vaddr_node);
  169. }
  170. goto out;
  171. }
  172. /* try to free some address space if we reached the limit */
  173. gk20a_instmem_vaddr_gc(imem, size);
  174. /* map the pages */
  175. node->base.vaddr = vmap(node->pages, size >> PAGE_SHIFT, VM_MAP,
  176. pgprot_writecombine(PAGE_KERNEL));
  177. if (!node->base.vaddr) {
  178. nvkm_error(&imem->base.subdev, "cannot map instobj - "
  179. "this is not going to end well...\n");
  180. goto out;
  181. }
  182. imem->vaddr_use += size;
  183. nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n",
  184. imem->vaddr_use, imem->vaddr_max);
  185. out:
  186. node->use_cpt++;
  187. mutex_unlock(&imem->lock);
  188. return node->base.vaddr;
  189. }
  190. static void
  191. gk20a_instobj_release_dma(struct nvkm_memory *memory)
  192. {
  193. struct gk20a_instobj *node = gk20a_instobj(memory);
  194. struct gk20a_instmem *imem = node->imem;
  195. struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
  196. /* in case we got a write-combined mapping */
  197. wmb();
  198. nvkm_ltc_invalidate(ltc);
  199. }
  200. static void
  201. gk20a_instobj_release_iommu(struct nvkm_memory *memory)
  202. {
  203. struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
  204. struct gk20a_instmem *imem = node->base.imem;
  205. struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
  206. mutex_lock(&imem->lock);
  207. /* we should at least have one user to release... */
  208. if (WARN_ON(node->use_cpt == 0))
  209. goto out;
  210. /* add unused objs to the LRU list to recycle their mapping */
  211. if (--node->use_cpt == 0)
  212. list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
  213. out:
  214. mutex_unlock(&imem->lock);
  215. wmb();
  216. nvkm_ltc_invalidate(ltc);
  217. }
  218. static u32
  219. gk20a_instobj_rd32(struct nvkm_memory *memory, u64 offset)
  220. {
  221. struct gk20a_instobj *node = gk20a_instobj(memory);
  222. return node->vaddr[offset / 4];
  223. }
  224. static void
  225. gk20a_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
  226. {
  227. struct gk20a_instobj *node = gk20a_instobj(memory);
  228. node->vaddr[offset / 4] = data;
  229. }
  230. static void
  231. gk20a_instobj_map(struct nvkm_memory *memory, struct nvkm_vma *vma, u64 offset)
  232. {
  233. struct gk20a_instobj *node = gk20a_instobj(memory);
  234. nvkm_vm_map_at(vma, offset, &node->mem);
  235. }
  236. static void *
  237. gk20a_instobj_dtor_dma(struct nvkm_memory *memory)
  238. {
  239. struct gk20a_instobj_dma *node = gk20a_instobj_dma(memory);
  240. struct gk20a_instmem *imem = node->base.imem;
  241. struct device *dev = imem->base.subdev.device->dev;
  242. if (unlikely(!node->base.vaddr))
  243. goto out;
  244. dma_free_attrs(dev, node->base.mem.size << PAGE_SHIFT, node->base.vaddr,
  245. node->handle, imem->attrs);
  246. out:
  247. return node;
  248. }
  249. static void *
  250. gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
  251. {
  252. struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
  253. struct gk20a_instmem *imem = node->base.imem;
  254. struct device *dev = imem->base.subdev.device->dev;
  255. struct nvkm_mm_node *r = node->base.mem.mem;
  256. int i;
  257. if (unlikely(!r))
  258. goto out;
  259. mutex_lock(&imem->lock);
  260. /* vaddr has already been recycled */
  261. if (node->base.vaddr)
  262. gk20a_instobj_iommu_recycle_vaddr(node);
  263. mutex_unlock(&imem->lock);
  264. /* clear IOMMU bit to unmap pages */
  265. r->offset &= ~BIT(imem->iommu_bit - imem->iommu_pgshift);
  266. /* Unmap pages from GPU address space and free them */
  267. for (i = 0; i < node->base.mem.size; i++) {
  268. iommu_unmap(imem->domain,
  269. (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
  270. dma_unmap_page(dev, node->dma_addrs[i], PAGE_SIZE,
  271. DMA_BIDIRECTIONAL);
  272. __free_page(node->pages[i]);
  273. }
  274. /* Release area from GPU address space */
  275. mutex_lock(imem->mm_mutex);
  276. nvkm_mm_free(imem->mm, &r);
  277. mutex_unlock(imem->mm_mutex);
  278. out:
  279. return node;
  280. }
  281. static const struct nvkm_memory_func
  282. gk20a_instobj_func_dma = {
  283. .dtor = gk20a_instobj_dtor_dma,
  284. .target = gk20a_instobj_target,
  285. .addr = gk20a_instobj_addr,
  286. .size = gk20a_instobj_size,
  287. .acquire = gk20a_instobj_acquire_dma,
  288. .release = gk20a_instobj_release_dma,
  289. .rd32 = gk20a_instobj_rd32,
  290. .wr32 = gk20a_instobj_wr32,
  291. .map = gk20a_instobj_map,
  292. };
  293. static const struct nvkm_memory_func
  294. gk20a_instobj_func_iommu = {
  295. .dtor = gk20a_instobj_dtor_iommu,
  296. .target = gk20a_instobj_target,
  297. .addr = gk20a_instobj_addr,
  298. .size = gk20a_instobj_size,
  299. .acquire = gk20a_instobj_acquire_iommu,
  300. .release = gk20a_instobj_release_iommu,
  301. .rd32 = gk20a_instobj_rd32,
  302. .wr32 = gk20a_instobj_wr32,
  303. .map = gk20a_instobj_map,
  304. };
  305. static int
  306. gk20a_instobj_ctor_dma(struct gk20a_instmem *imem, u32 npages, u32 align,
  307. struct gk20a_instobj **_node)
  308. {
  309. struct gk20a_instobj_dma *node;
  310. struct nvkm_subdev *subdev = &imem->base.subdev;
  311. struct device *dev = subdev->device->dev;
  312. if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
  313. return -ENOMEM;
  314. *_node = &node->base;
  315. nvkm_memory_ctor(&gk20a_instobj_func_dma, &node->base.memory);
  316. node->base.vaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
  317. &node->handle, GFP_KERNEL,
  318. imem->attrs);
  319. if (!node->base.vaddr) {
  320. nvkm_error(subdev, "cannot allocate DMA memory\n");
  321. return -ENOMEM;
  322. }
  323. /* alignment check */
  324. if (unlikely(node->handle & (align - 1)))
  325. nvkm_warn(subdev,
  326. "memory not aligned as requested: %pad (0x%x)\n",
  327. &node->handle, align);
  328. /* present memory for being mapped using small pages */
  329. node->r.type = 12;
  330. node->r.offset = node->handle >> 12;
  331. node->r.length = (npages << PAGE_SHIFT) >> 12;
  332. node->base.mem.offset = node->handle;
  333. node->base.mem.mem = &node->r;
  334. return 0;
  335. }
  336. static int
  337. gk20a_instobj_ctor_iommu(struct gk20a_instmem *imem, u32 npages, u32 align,
  338. struct gk20a_instobj **_node)
  339. {
  340. struct gk20a_instobj_iommu *node;
  341. struct nvkm_subdev *subdev = &imem->base.subdev;
  342. struct device *dev = subdev->device->dev;
  343. struct nvkm_mm_node *r;
  344. int ret;
  345. int i;
  346. /*
  347. * despite their variable size, instmem allocations are small enough
  348. * (< 1 page) to be handled by kzalloc
  349. */
  350. if (!(node = kzalloc(sizeof(*node) + ((sizeof(node->pages[0]) +
  351. sizeof(*node->dma_addrs)) * npages), GFP_KERNEL)))
  352. return -ENOMEM;
  353. *_node = &node->base;
  354. node->dma_addrs = (void *)(node->pages + npages);
  355. nvkm_memory_ctor(&gk20a_instobj_func_iommu, &node->base.memory);
  356. /* Allocate backing memory */
  357. for (i = 0; i < npages; i++) {
  358. struct page *p = alloc_page(GFP_KERNEL);
  359. dma_addr_t dma_adr;
  360. if (p == NULL) {
  361. ret = -ENOMEM;
  362. goto free_pages;
  363. }
  364. node->pages[i] = p;
  365. dma_adr = dma_map_page(dev, p, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
  366. if (dma_mapping_error(dev, dma_adr)) {
  367. nvkm_error(subdev, "DMA mapping error!\n");
  368. ret = -ENOMEM;
  369. goto free_pages;
  370. }
  371. node->dma_addrs[i] = dma_adr;
  372. }
  373. mutex_lock(imem->mm_mutex);
  374. /* Reserve area from GPU address space */
  375. ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
  376. align >> imem->iommu_pgshift, &r);
  377. mutex_unlock(imem->mm_mutex);
  378. if (ret) {
  379. nvkm_error(subdev, "IOMMU space is full!\n");
  380. goto free_pages;
  381. }
  382. /* Map into GPU address space */
  383. for (i = 0; i < npages; i++) {
  384. u32 offset = (r->offset + i) << imem->iommu_pgshift;
  385. ret = iommu_map(imem->domain, offset, node->dma_addrs[i],
  386. PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
  387. if (ret < 0) {
  388. nvkm_error(subdev, "IOMMU mapping failure: %d\n", ret);
  389. while (i-- > 0) {
  390. offset -= PAGE_SIZE;
  391. iommu_unmap(imem->domain, offset, PAGE_SIZE);
  392. }
  393. goto release_area;
  394. }
  395. }
  396. /* IOMMU bit tells that an address is to be resolved through the IOMMU */
  397. r->offset |= BIT(imem->iommu_bit - imem->iommu_pgshift);
  398. node->base.mem.offset = ((u64)r->offset) << imem->iommu_pgshift;
  399. node->base.mem.mem = r;
  400. return 0;
  401. release_area:
  402. mutex_lock(imem->mm_mutex);
  403. nvkm_mm_free(imem->mm, &r);
  404. mutex_unlock(imem->mm_mutex);
  405. free_pages:
  406. for (i = 0; i < npages && node->pages[i] != NULL; i++) {
  407. dma_addr_t dma_addr = node->dma_addrs[i];
  408. if (dma_addr)
  409. dma_unmap_page(dev, dma_addr, PAGE_SIZE,
  410. DMA_BIDIRECTIONAL);
  411. __free_page(node->pages[i]);
  412. }
  413. return ret;
  414. }
  415. static int
  416. gk20a_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
  417. struct nvkm_memory **pmemory)
  418. {
  419. struct gk20a_instmem *imem = gk20a_instmem(base);
  420. struct nvkm_subdev *subdev = &imem->base.subdev;
  421. struct gk20a_instobj *node = NULL;
  422. int ret;
  423. nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
  424. imem->domain ? "IOMMU" : "DMA", size, align);
  425. /* Round size and align to page bounds */
  426. size = max(roundup(size, PAGE_SIZE), PAGE_SIZE);
  427. align = max(roundup(align, PAGE_SIZE), PAGE_SIZE);
  428. if (imem->domain)
  429. ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT,
  430. align, &node);
  431. else
  432. ret = gk20a_instobj_ctor_dma(imem, size >> PAGE_SHIFT,
  433. align, &node);
  434. *pmemory = node ? &node->memory : NULL;
  435. if (ret)
  436. return ret;
  437. node->imem = imem;
  438. /* present memory for being mapped using small pages */
  439. node->mem.size = size >> 12;
  440. node->mem.memtype = 0;
  441. node->mem.page_shift = 12;
  442. nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
  443. size, align, node->mem.offset);
  444. return 0;
  445. }
  446. static void *
  447. gk20a_instmem_dtor(struct nvkm_instmem *base)
  448. {
  449. struct gk20a_instmem *imem = gk20a_instmem(base);
  450. /* perform some sanity checks... */
  451. if (!list_empty(&imem->vaddr_lru))
  452. nvkm_warn(&base->subdev, "instobj LRU not empty!\n");
  453. if (imem->vaddr_use != 0)
  454. nvkm_warn(&base->subdev, "instobj vmap area not empty! "
  455. "0x%x bytes still mapped\n", imem->vaddr_use);
  456. return imem;
  457. }
  458. static const struct nvkm_instmem_func
  459. gk20a_instmem = {
  460. .dtor = gk20a_instmem_dtor,
  461. .memory_new = gk20a_instobj_new,
  462. .persistent = true,
  463. .zero = false,
  464. };
  465. int
  466. gk20a_instmem_new(struct nvkm_device *device, int index,
  467. struct nvkm_instmem **pimem)
  468. {
  469. struct nvkm_device_tegra *tdev = device->func->tegra(device);
  470. struct gk20a_instmem *imem;
  471. if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
  472. return -ENOMEM;
  473. nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
  474. mutex_init(&imem->lock);
  475. *pimem = &imem->base;
  476. /* do not allow more than 1MB of CPU-mapped instmem */
  477. imem->vaddr_use = 0;
  478. imem->vaddr_max = 0x100000;
  479. INIT_LIST_HEAD(&imem->vaddr_lru);
  480. if (tdev->iommu.domain) {
  481. imem->mm_mutex = &tdev->iommu.mutex;
  482. imem->mm = &tdev->iommu.mm;
  483. imem->domain = tdev->iommu.domain;
  484. imem->iommu_pgshift = tdev->iommu.pgshift;
  485. imem->iommu_bit = tdev->func->iommu_bit;
  486. nvkm_info(&imem->base.subdev, "using IOMMU\n");
  487. } else {
  488. imem->attrs = DMA_ATTR_NON_CONSISTENT |
  489. DMA_ATTR_WEAK_ORDERING |
  490. DMA_ATTR_WRITE_COMBINE;
  491. nvkm_info(&imem->base.subdev, "using DMA API\n");
  492. }
  493. return 0;
  494. }