gk20a.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 providing
  26. * coherent read and write operations.
  27. *
  28. * Instmem can be allocated through two means:
  29. * 1) If an IOMMU mapping 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 mapping is probed, the DMA API is used to allocate physically
  32. * contiguous memory.
  33. *
  34. * In both cases CPU read and writes are performed using PRAMIN (i.e. using the
  35. * GPU path) to ensure these operations are coherent for the GPU. This allows us
  36. * to use more "relaxed" allocation parameters when using the DMA API, since we
  37. * never need a kernel mapping.
  38. */
  39. #define gk20a_instmem(p) container_of((p), struct gk20a_instmem, base)
  40. #include "priv.h"
  41. #include <core/memory.h>
  42. #include <core/mm.h>
  43. #include <core/tegra.h>
  44. #include <subdev/fb.h>
  45. #define gk20a_instobj(p) container_of((p), struct gk20a_instobj, memory)
  46. struct gk20a_instobj {
  47. struct nvkm_memory memory;
  48. struct gk20a_instmem *imem;
  49. struct nvkm_mem mem;
  50. };
  51. /*
  52. * Used for objects allocated using the DMA API
  53. */
  54. struct gk20a_instobj_dma {
  55. struct gk20a_instobj base;
  56. void *cpuaddr;
  57. dma_addr_t handle;
  58. struct nvkm_mm_node r;
  59. };
  60. /*
  61. * Used for objects flattened using the IOMMU API
  62. */
  63. struct gk20a_instobj_iommu {
  64. struct gk20a_instobj base;
  65. /* array of base.mem->size pages */
  66. struct page *pages[];
  67. };
  68. struct gk20a_instmem {
  69. struct nvkm_instmem base;
  70. unsigned long lock_flags;
  71. spinlock_t lock;
  72. u64 addr;
  73. /* Only used if IOMMU if present */
  74. struct mutex *mm_mutex;
  75. struct nvkm_mm *mm;
  76. struct iommu_domain *domain;
  77. unsigned long iommu_pgshift;
  78. /* Only used by DMA API */
  79. struct dma_attrs attrs;
  80. };
  81. static enum nvkm_memory_target
  82. gk20a_instobj_target(struct nvkm_memory *memory)
  83. {
  84. return NVKM_MEM_TARGET_HOST;
  85. }
  86. static u64
  87. gk20a_instobj_addr(struct nvkm_memory *memory)
  88. {
  89. return gk20a_instobj(memory)->mem.offset;
  90. }
  91. static u64
  92. gk20a_instobj_size(struct nvkm_memory *memory)
  93. {
  94. return (u64)gk20a_instobj(memory)->mem.size << 12;
  95. }
  96. static void __iomem *
  97. gk20a_instobj_acquire(struct nvkm_memory *memory)
  98. {
  99. struct gk20a_instmem *imem = gk20a_instobj(memory)->imem;
  100. unsigned long flags;
  101. spin_lock_irqsave(&imem->lock, flags);
  102. imem->lock_flags = flags;
  103. return NULL;
  104. }
  105. static void
  106. gk20a_instobj_release(struct nvkm_memory *memory)
  107. {
  108. struct gk20a_instmem *imem = gk20a_instobj(memory)->imem;
  109. spin_unlock_irqrestore(&imem->lock, imem->lock_flags);
  110. }
  111. /*
  112. * Use PRAMIN to read/write data and avoid coherency issues.
  113. * PRAMIN uses the GPU path and ensures data will always be coherent.
  114. *
  115. * A dynamic mapping based solution would be desirable in the future, but
  116. * the issue remains of how to maintain coherency efficiently. On ARM it is
  117. * not easy (if possible at all?) to create uncached temporary mappings.
  118. */
  119. static u32
  120. gk20a_instobj_rd32(struct nvkm_memory *memory, u64 offset)
  121. {
  122. struct gk20a_instobj *node = gk20a_instobj(memory);
  123. struct gk20a_instmem *imem = node->imem;
  124. struct nvkm_device *device = imem->base.subdev.device;
  125. u64 base = (node->mem.offset + offset) & 0xffffff00000ULL;
  126. u64 addr = (node->mem.offset + offset) & 0x000000fffffULL;
  127. u32 data;
  128. if (unlikely(imem->addr != base)) {
  129. nvkm_wr32(device, 0x001700, base >> 16);
  130. imem->addr = base;
  131. }
  132. data = nvkm_rd32(device, 0x700000 + addr);
  133. return data;
  134. }
  135. static void
  136. gk20a_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
  137. {
  138. struct gk20a_instobj *node = gk20a_instobj(memory);
  139. struct gk20a_instmem *imem = node->imem;
  140. struct nvkm_device *device = imem->base.subdev.device;
  141. u64 base = (node->mem.offset + offset) & 0xffffff00000ULL;
  142. u64 addr = (node->mem.offset + offset) & 0x000000fffffULL;
  143. if (unlikely(imem->addr != base)) {
  144. nvkm_wr32(device, 0x001700, base >> 16);
  145. imem->addr = base;
  146. }
  147. nvkm_wr32(device, 0x700000 + addr, data);
  148. }
  149. static void
  150. gk20a_instobj_map(struct nvkm_memory *memory, struct nvkm_vma *vma, u64 offset)
  151. {
  152. struct gk20a_instobj *node = gk20a_instobj(memory);
  153. nvkm_vm_map_at(vma, offset, &node->mem);
  154. }
  155. static void
  156. gk20a_instobj_dtor_dma(struct gk20a_instobj *_node)
  157. {
  158. struct gk20a_instobj_dma *node = (void *)_node;
  159. struct gk20a_instmem *imem = _node->imem;
  160. struct device *dev = imem->base.subdev.device->dev;
  161. if (unlikely(!node->cpuaddr))
  162. return;
  163. dma_free_attrs(dev, _node->mem.size << PAGE_SHIFT, node->cpuaddr,
  164. node->handle, &imem->attrs);
  165. }
  166. static void
  167. gk20a_instobj_dtor_iommu(struct gk20a_instobj *_node)
  168. {
  169. struct gk20a_instobj_iommu *node = (void *)_node;
  170. struct gk20a_instmem *imem = _node->imem;
  171. struct nvkm_mm_node *r;
  172. int i;
  173. if (unlikely(list_empty(&_node->mem.regions)))
  174. return;
  175. r = list_first_entry(&_node->mem.regions, struct nvkm_mm_node,
  176. rl_entry);
  177. /* clear bit 34 to unmap pages */
  178. r->offset &= ~BIT(34 - imem->iommu_pgshift);
  179. /* Unmap pages from GPU address space and free them */
  180. for (i = 0; i < _node->mem.size; i++) {
  181. iommu_unmap(imem->domain,
  182. (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
  183. __free_page(node->pages[i]);
  184. }
  185. /* Release area from GPU address space */
  186. mutex_lock(imem->mm_mutex);
  187. nvkm_mm_free(imem->mm, &r);
  188. mutex_unlock(imem->mm_mutex);
  189. }
  190. static void *
  191. gk20a_instobj_dtor(struct nvkm_memory *memory)
  192. {
  193. struct gk20a_instobj *node = gk20a_instobj(memory);
  194. struct gk20a_instmem *imem = node->imem;
  195. if (imem->domain)
  196. gk20a_instobj_dtor_iommu(node);
  197. else
  198. gk20a_instobj_dtor_dma(node);
  199. return node;
  200. }
  201. static const struct nvkm_memory_func
  202. gk20a_instobj_func = {
  203. .dtor = gk20a_instobj_dtor,
  204. .target = gk20a_instobj_target,
  205. .addr = gk20a_instobj_addr,
  206. .size = gk20a_instobj_size,
  207. .acquire = gk20a_instobj_acquire,
  208. .release = gk20a_instobj_release,
  209. .rd32 = gk20a_instobj_rd32,
  210. .wr32 = gk20a_instobj_wr32,
  211. .map = gk20a_instobj_map,
  212. };
  213. static int
  214. gk20a_instobj_ctor_dma(struct gk20a_instmem *imem, u32 npages, u32 align,
  215. struct gk20a_instobj **_node)
  216. {
  217. struct gk20a_instobj_dma *node;
  218. struct nvkm_subdev *subdev = &imem->base.subdev;
  219. struct device *dev = subdev->device->dev;
  220. if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
  221. return -ENOMEM;
  222. *_node = &node->base;
  223. node->cpuaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
  224. &node->handle, GFP_KERNEL,
  225. &imem->attrs);
  226. if (!node->cpuaddr) {
  227. nvkm_error(subdev, "cannot allocate DMA memory\n");
  228. return -ENOMEM;
  229. }
  230. /* alignment check */
  231. if (unlikely(node->handle & (align - 1)))
  232. nvkm_warn(subdev,
  233. "memory not aligned as requested: %pad (0x%x)\n",
  234. &node->handle, align);
  235. /* present memory for being mapped using small pages */
  236. node->r.type = 12;
  237. node->r.offset = node->handle >> 12;
  238. node->r.length = (npages << PAGE_SHIFT) >> 12;
  239. node->base.mem.offset = node->handle;
  240. INIT_LIST_HEAD(&node->base.mem.regions);
  241. list_add_tail(&node->r.rl_entry, &node->base.mem.regions);
  242. return 0;
  243. }
  244. static int
  245. gk20a_instobj_ctor_iommu(struct gk20a_instmem *imem, u32 npages, u32 align,
  246. struct gk20a_instobj **_node)
  247. {
  248. struct gk20a_instobj_iommu *node;
  249. struct nvkm_subdev *subdev = &imem->base.subdev;
  250. struct nvkm_mm_node *r;
  251. int ret;
  252. int i;
  253. if (!(node = kzalloc(sizeof(*node) +
  254. sizeof( node->pages[0]) * npages, GFP_KERNEL)))
  255. return -ENOMEM;
  256. *_node = &node->base;
  257. /* Allocate backing memory */
  258. for (i = 0; i < npages; i++) {
  259. struct page *p = alloc_page(GFP_KERNEL);
  260. if (p == NULL) {
  261. ret = -ENOMEM;
  262. goto free_pages;
  263. }
  264. node->pages[i] = p;
  265. }
  266. mutex_lock(imem->mm_mutex);
  267. /* Reserve area from GPU address space */
  268. ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
  269. align >> imem->iommu_pgshift, &r);
  270. mutex_unlock(imem->mm_mutex);
  271. if (ret) {
  272. nvkm_error(subdev, "virtual space is full!\n");
  273. goto free_pages;
  274. }
  275. /* Map into GPU address space */
  276. for (i = 0; i < npages; i++) {
  277. struct page *p = node->pages[i];
  278. u32 offset = (r->offset + i) << imem->iommu_pgshift;
  279. ret = iommu_map(imem->domain, offset, page_to_phys(p),
  280. PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
  281. if (ret < 0) {
  282. nvkm_error(subdev, "IOMMU mapping failure: %d\n", ret);
  283. while (i-- > 0) {
  284. offset -= PAGE_SIZE;
  285. iommu_unmap(imem->domain, offset, PAGE_SIZE);
  286. }
  287. goto release_area;
  288. }
  289. }
  290. /* Bit 34 tells that an address is to be resolved through the IOMMU */
  291. r->offset |= BIT(34 - imem->iommu_pgshift);
  292. node->base.mem.offset = ((u64)r->offset) << imem->iommu_pgshift;
  293. INIT_LIST_HEAD(&node->base.mem.regions);
  294. list_add_tail(&r->rl_entry, &node->base.mem.regions);
  295. return 0;
  296. release_area:
  297. mutex_lock(imem->mm_mutex);
  298. nvkm_mm_free(imem->mm, &r);
  299. mutex_unlock(imem->mm_mutex);
  300. free_pages:
  301. for (i = 0; i < npages && node->pages[i] != NULL; i++)
  302. __free_page(node->pages[i]);
  303. return ret;
  304. }
  305. static int
  306. gk20a_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
  307. struct nvkm_memory **pmemory)
  308. {
  309. struct gk20a_instmem *imem = gk20a_instmem(base);
  310. struct gk20a_instobj *node = NULL;
  311. struct nvkm_subdev *subdev = &imem->base.subdev;
  312. int ret;
  313. nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
  314. imem->domain ? "IOMMU" : "DMA", size, align);
  315. /* Round size and align to page bounds */
  316. size = max(roundup(size, PAGE_SIZE), PAGE_SIZE);
  317. align = max(roundup(align, PAGE_SIZE), PAGE_SIZE);
  318. if (imem->domain)
  319. ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT,
  320. align, &node);
  321. else
  322. ret = gk20a_instobj_ctor_dma(imem, size >> PAGE_SHIFT,
  323. align, &node);
  324. *pmemory = node ? &node->memory : NULL;
  325. if (ret)
  326. return ret;
  327. nvkm_memory_ctor(&gk20a_instobj_func, &node->memory);
  328. node->imem = imem;
  329. /* present memory for being mapped using small pages */
  330. node->mem.size = size >> 12;
  331. node->mem.memtype = 0;
  332. node->mem.page_shift = 12;
  333. nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
  334. size, align, node->mem.offset);
  335. return 0;
  336. }
  337. static void
  338. gk20a_instmem_fini(struct nvkm_instmem *base)
  339. {
  340. gk20a_instmem(base)->addr = ~0ULL;
  341. }
  342. static const struct nvkm_instmem_func
  343. gk20a_instmem = {
  344. .fini = gk20a_instmem_fini,
  345. .memory_new = gk20a_instobj_new,
  346. .persistent = true,
  347. .zero = false,
  348. };
  349. int
  350. gk20a_instmem_new(struct nvkm_device *device, int index,
  351. struct nvkm_instmem **pimem)
  352. {
  353. struct nvkm_device_tegra *tdev = device->func->tegra(device);
  354. struct gk20a_instmem *imem;
  355. if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
  356. return -ENOMEM;
  357. nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
  358. spin_lock_init(&imem->lock);
  359. *pimem = &imem->base;
  360. if (tdev->iommu.domain) {
  361. imem->domain = tdev->iommu.domain;
  362. imem->mm = &tdev->iommu.mm;
  363. imem->iommu_pgshift = tdev->iommu.pgshift;
  364. imem->mm_mutex = &tdev->iommu.mutex;
  365. nvkm_info(&imem->base.subdev, "using IOMMU\n");
  366. } else {
  367. init_dma_attrs(&imem->attrs);
  368. /*
  369. * We will access instmem through PRAMIN and thus do not need a
  370. * consistent CPU pointer or kernel mapping
  371. */
  372. dma_set_attr(DMA_ATTR_NON_CONSISTENT, &imem->attrs);
  373. dma_set_attr(DMA_ATTR_WEAK_ORDERING, &imem->attrs);
  374. dma_set_attr(DMA_ATTR_WRITE_COMBINE, &imem->attrs);
  375. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &imem->attrs);
  376. nvkm_info(&imem->base.subdev, "using DMA API\n");
  377. }
  378. return 0;
  379. }