msm_gem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/spinlock.h>
  18. #include <linux/shmem_fs.h>
  19. #include <linux/dma-buf.h>
  20. #include "msm_drv.h"
  21. #include "msm_gem.h"
  22. #include "msm_gpu.h"
  23. #include "msm_mmu.h"
  24. static dma_addr_t physaddr(struct drm_gem_object *obj)
  25. {
  26. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  27. struct msm_drm_private *priv = obj->dev->dev_private;
  28. return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
  29. priv->vram.paddr;
  30. }
  31. static bool use_pages(struct drm_gem_object *obj)
  32. {
  33. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  34. return !msm_obj->vram_node;
  35. }
  36. /* allocate pages from VRAM carveout, used when no IOMMU: */
  37. static struct page **get_pages_vram(struct drm_gem_object *obj,
  38. int npages)
  39. {
  40. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  41. struct msm_drm_private *priv = obj->dev->dev_private;
  42. dma_addr_t paddr;
  43. struct page **p;
  44. int ret, i;
  45. p = drm_malloc_ab(npages, sizeof(struct page *));
  46. if (!p)
  47. return ERR_PTR(-ENOMEM);
  48. ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node,
  49. npages, 0, DRM_MM_SEARCH_DEFAULT);
  50. if (ret) {
  51. drm_free_large(p);
  52. return ERR_PTR(ret);
  53. }
  54. paddr = physaddr(obj);
  55. for (i = 0; i < npages; i++) {
  56. p[i] = phys_to_page(paddr);
  57. paddr += PAGE_SIZE;
  58. }
  59. return p;
  60. }
  61. /* called with dev->struct_mutex held */
  62. static struct page **get_pages(struct drm_gem_object *obj)
  63. {
  64. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  65. if (!msm_obj->pages) {
  66. struct drm_device *dev = obj->dev;
  67. struct page **p;
  68. int npages = obj->size >> PAGE_SHIFT;
  69. if (use_pages(obj))
  70. p = drm_gem_get_pages(obj);
  71. else
  72. p = get_pages_vram(obj, npages);
  73. if (IS_ERR(p)) {
  74. dev_err(dev->dev, "could not get pages: %ld\n",
  75. PTR_ERR(p));
  76. return p;
  77. }
  78. msm_obj->pages = p;
  79. msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
  80. if (IS_ERR(msm_obj->sgt)) {
  81. void *ptr = ERR_CAST(msm_obj->sgt);
  82. dev_err(dev->dev, "failed to allocate sgt\n");
  83. msm_obj->sgt = NULL;
  84. return ptr;
  85. }
  86. /* For non-cached buffers, ensure the new pages are clean
  87. * because display controller, GPU, etc. are not coherent:
  88. */
  89. if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
  90. dma_map_sg(dev->dev, msm_obj->sgt->sgl,
  91. msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
  92. }
  93. return msm_obj->pages;
  94. }
  95. static void put_pages(struct drm_gem_object *obj)
  96. {
  97. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  98. if (msm_obj->pages) {
  99. /* For non-cached buffers, ensure the new pages are clean
  100. * because display controller, GPU, etc. are not coherent:
  101. */
  102. if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
  103. dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
  104. msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
  105. if (msm_obj->sgt)
  106. sg_free_table(msm_obj->sgt);
  107. kfree(msm_obj->sgt);
  108. if (use_pages(obj))
  109. drm_gem_put_pages(obj, msm_obj->pages, true, false);
  110. else {
  111. drm_mm_remove_node(msm_obj->vram_node);
  112. drm_free_large(msm_obj->pages);
  113. }
  114. msm_obj->pages = NULL;
  115. }
  116. }
  117. struct page **msm_gem_get_pages(struct drm_gem_object *obj)
  118. {
  119. struct drm_device *dev = obj->dev;
  120. struct page **p;
  121. mutex_lock(&dev->struct_mutex);
  122. p = get_pages(obj);
  123. mutex_unlock(&dev->struct_mutex);
  124. return p;
  125. }
  126. void msm_gem_put_pages(struct drm_gem_object *obj)
  127. {
  128. /* when we start tracking the pin count, then do something here */
  129. }
  130. int msm_gem_mmap_obj(struct drm_gem_object *obj,
  131. struct vm_area_struct *vma)
  132. {
  133. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  134. vma->vm_flags &= ~VM_PFNMAP;
  135. vma->vm_flags |= VM_MIXEDMAP;
  136. if (msm_obj->flags & MSM_BO_WC) {
  137. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  138. } else if (msm_obj->flags & MSM_BO_UNCACHED) {
  139. vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
  140. } else {
  141. /*
  142. * Shunt off cached objs to shmem file so they have their own
  143. * address_space (so unmap_mapping_range does what we want,
  144. * in particular in the case of mmap'd dmabufs)
  145. */
  146. fput(vma->vm_file);
  147. get_file(obj->filp);
  148. vma->vm_pgoff = 0;
  149. vma->vm_file = obj->filp;
  150. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  151. }
  152. return 0;
  153. }
  154. int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  155. {
  156. int ret;
  157. ret = drm_gem_mmap(filp, vma);
  158. if (ret) {
  159. DBG("mmap failed: %d", ret);
  160. return ret;
  161. }
  162. return msm_gem_mmap_obj(vma->vm_private_data, vma);
  163. }
  164. int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  165. {
  166. struct drm_gem_object *obj = vma->vm_private_data;
  167. struct drm_device *dev = obj->dev;
  168. struct page **pages;
  169. unsigned long pfn;
  170. pgoff_t pgoff;
  171. int ret;
  172. /* Make sure we don't parallel update on a fault, nor move or remove
  173. * something from beneath our feet
  174. */
  175. ret = mutex_lock_interruptible(&dev->struct_mutex);
  176. if (ret)
  177. goto out;
  178. /* make sure we have pages attached now */
  179. pages = get_pages(obj);
  180. if (IS_ERR(pages)) {
  181. ret = PTR_ERR(pages);
  182. goto out_unlock;
  183. }
  184. /* We don't use vmf->pgoff since that has the fake offset: */
  185. pgoff = ((unsigned long)vmf->virtual_address -
  186. vma->vm_start) >> PAGE_SHIFT;
  187. pfn = page_to_pfn(pages[pgoff]);
  188. VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
  189. pfn, pfn << PAGE_SHIFT);
  190. ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
  191. out_unlock:
  192. mutex_unlock(&dev->struct_mutex);
  193. out:
  194. switch (ret) {
  195. case -EAGAIN:
  196. case 0:
  197. case -ERESTARTSYS:
  198. case -EINTR:
  199. case -EBUSY:
  200. /*
  201. * EBUSY is ok: this just means that another thread
  202. * already did the job.
  203. */
  204. return VM_FAULT_NOPAGE;
  205. case -ENOMEM:
  206. return VM_FAULT_OOM;
  207. default:
  208. return VM_FAULT_SIGBUS;
  209. }
  210. }
  211. /** get mmap offset */
  212. static uint64_t mmap_offset(struct drm_gem_object *obj)
  213. {
  214. struct drm_device *dev = obj->dev;
  215. int ret;
  216. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  217. /* Make it mmapable */
  218. ret = drm_gem_create_mmap_offset(obj);
  219. if (ret) {
  220. dev_err(dev->dev, "could not allocate mmap offset\n");
  221. return 0;
  222. }
  223. return drm_vma_node_offset_addr(&obj->vma_node);
  224. }
  225. uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
  226. {
  227. uint64_t offset;
  228. mutex_lock(&obj->dev->struct_mutex);
  229. offset = mmap_offset(obj);
  230. mutex_unlock(&obj->dev->struct_mutex);
  231. return offset;
  232. }
  233. /* should be called under struct_mutex.. although it can be called
  234. * from atomic context without struct_mutex to acquire an extra
  235. * iova ref if you know one is already held.
  236. *
  237. * That means when I do eventually need to add support for unpinning
  238. * the refcnt counter needs to be atomic_t.
  239. */
  240. int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id,
  241. uint32_t *iova)
  242. {
  243. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  244. int ret = 0;
  245. if (!msm_obj->domain[id].iova) {
  246. struct msm_drm_private *priv = obj->dev->dev_private;
  247. struct page **pages = get_pages(obj);
  248. if (IS_ERR(pages))
  249. return PTR_ERR(pages);
  250. if (iommu_present(&platform_bus_type)) {
  251. struct msm_mmu *mmu = priv->mmus[id];
  252. uint32_t offset;
  253. if (WARN_ON(!mmu))
  254. return -EINVAL;
  255. offset = (uint32_t)mmap_offset(obj);
  256. ret = mmu->funcs->map(mmu, offset, msm_obj->sgt,
  257. obj->size, IOMMU_READ | IOMMU_WRITE);
  258. msm_obj->domain[id].iova = offset;
  259. } else {
  260. msm_obj->domain[id].iova = physaddr(obj);
  261. }
  262. }
  263. if (!ret)
  264. *iova = msm_obj->domain[id].iova;
  265. return ret;
  266. }
  267. /* get iova, taking a reference. Should have a matching put */
  268. int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova)
  269. {
  270. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  271. int ret;
  272. /* this is safe right now because we don't unmap until the
  273. * bo is deleted:
  274. */
  275. if (msm_obj->domain[id].iova) {
  276. *iova = msm_obj->domain[id].iova;
  277. return 0;
  278. }
  279. mutex_lock(&obj->dev->struct_mutex);
  280. ret = msm_gem_get_iova_locked(obj, id, iova);
  281. mutex_unlock(&obj->dev->struct_mutex);
  282. return ret;
  283. }
  284. /* get iova without taking a reference, used in places where you have
  285. * already done a 'msm_gem_get_iova()'.
  286. */
  287. uint32_t msm_gem_iova(struct drm_gem_object *obj, int id)
  288. {
  289. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  290. WARN_ON(!msm_obj->domain[id].iova);
  291. return msm_obj->domain[id].iova;
  292. }
  293. void msm_gem_put_iova(struct drm_gem_object *obj, int id)
  294. {
  295. // XXX TODO ..
  296. // NOTE: probably don't need a _locked() version.. we wouldn't
  297. // normally unmap here, but instead just mark that it could be
  298. // unmapped (if the iova refcnt drops to zero), but then later
  299. // if another _get_iova_locked() fails we can start unmapping
  300. // things that are no longer needed..
  301. }
  302. int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  303. struct drm_mode_create_dumb *args)
  304. {
  305. args->pitch = align_pitch(args->width, args->bpp);
  306. args->size = PAGE_ALIGN(args->pitch * args->height);
  307. return msm_gem_new_handle(dev, file, args->size,
  308. MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
  309. }
  310. int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  311. uint32_t handle, uint64_t *offset)
  312. {
  313. struct drm_gem_object *obj;
  314. int ret = 0;
  315. /* GEM does all our handle to object mapping */
  316. obj = drm_gem_object_lookup(dev, file, handle);
  317. if (obj == NULL) {
  318. ret = -ENOENT;
  319. goto fail;
  320. }
  321. *offset = msm_gem_mmap_offset(obj);
  322. drm_gem_object_unreference_unlocked(obj);
  323. fail:
  324. return ret;
  325. }
  326. void *msm_gem_vaddr_locked(struct drm_gem_object *obj)
  327. {
  328. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  329. WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
  330. if (!msm_obj->vaddr) {
  331. struct page **pages = get_pages(obj);
  332. if (IS_ERR(pages))
  333. return ERR_CAST(pages);
  334. msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
  335. VM_MAP, pgprot_writecombine(PAGE_KERNEL));
  336. }
  337. return msm_obj->vaddr;
  338. }
  339. void *msm_gem_vaddr(struct drm_gem_object *obj)
  340. {
  341. void *ret;
  342. mutex_lock(&obj->dev->struct_mutex);
  343. ret = msm_gem_vaddr_locked(obj);
  344. mutex_unlock(&obj->dev->struct_mutex);
  345. return ret;
  346. }
  347. /* setup callback for when bo is no longer busy..
  348. * TODO probably want to differentiate read vs write..
  349. */
  350. int msm_gem_queue_inactive_cb(struct drm_gem_object *obj,
  351. struct msm_fence_cb *cb)
  352. {
  353. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  354. uint32_t fence = msm_gem_fence(msm_obj,
  355. MSM_PREP_READ | MSM_PREP_WRITE);
  356. return msm_queue_fence_cb(obj->dev, cb, fence);
  357. }
  358. void msm_gem_move_to_active(struct drm_gem_object *obj,
  359. struct msm_gpu *gpu, bool write, uint32_t fence)
  360. {
  361. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  362. msm_obj->gpu = gpu;
  363. if (write)
  364. msm_obj->write_fence = fence;
  365. else
  366. msm_obj->read_fence = fence;
  367. list_del_init(&msm_obj->mm_list);
  368. list_add_tail(&msm_obj->mm_list, &gpu->active_list);
  369. }
  370. void msm_gem_move_to_inactive(struct drm_gem_object *obj)
  371. {
  372. struct drm_device *dev = obj->dev;
  373. struct msm_drm_private *priv = dev->dev_private;
  374. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  375. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  376. msm_obj->gpu = NULL;
  377. msm_obj->read_fence = 0;
  378. msm_obj->write_fence = 0;
  379. list_del_init(&msm_obj->mm_list);
  380. list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
  381. }
  382. int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
  383. {
  384. struct drm_device *dev = obj->dev;
  385. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  386. int ret = 0;
  387. if (is_active(msm_obj)) {
  388. uint32_t fence = msm_gem_fence(msm_obj, op);
  389. if (op & MSM_PREP_NOSYNC)
  390. timeout = NULL;
  391. ret = msm_wait_fence(dev, fence, timeout, true);
  392. }
  393. /* TODO cache maintenance */
  394. return ret;
  395. }
  396. int msm_gem_cpu_fini(struct drm_gem_object *obj)
  397. {
  398. /* TODO cache maintenance */
  399. return 0;
  400. }
  401. #ifdef CONFIG_DEBUG_FS
  402. void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
  403. {
  404. struct drm_device *dev = obj->dev;
  405. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  406. uint64_t off = drm_vma_node_start(&obj->vma_node);
  407. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  408. seq_printf(m, "%08x: %c(r=%u,w=%u) %2d (%2d) %08llx %p %zu\n",
  409. msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
  410. msm_obj->read_fence, msm_obj->write_fence,
  411. obj->name, obj->refcount.refcount.counter,
  412. off, msm_obj->vaddr, obj->size);
  413. }
  414. void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
  415. {
  416. struct msm_gem_object *msm_obj;
  417. int count = 0;
  418. size_t size = 0;
  419. list_for_each_entry(msm_obj, list, mm_list) {
  420. struct drm_gem_object *obj = &msm_obj->base;
  421. seq_printf(m, " ");
  422. msm_gem_describe(obj, m);
  423. count++;
  424. size += obj->size;
  425. }
  426. seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
  427. }
  428. #endif
  429. void msm_gem_free_object(struct drm_gem_object *obj)
  430. {
  431. struct drm_device *dev = obj->dev;
  432. struct msm_drm_private *priv = obj->dev->dev_private;
  433. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  434. int id;
  435. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  436. /* object should not be on active list: */
  437. WARN_ON(is_active(msm_obj));
  438. list_del(&msm_obj->mm_list);
  439. for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) {
  440. struct msm_mmu *mmu = priv->mmus[id];
  441. if (mmu && msm_obj->domain[id].iova) {
  442. uint32_t offset = msm_obj->domain[id].iova;
  443. mmu->funcs->unmap(mmu, offset, msm_obj->sgt, obj->size);
  444. }
  445. }
  446. if (obj->import_attach) {
  447. if (msm_obj->vaddr)
  448. dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
  449. /* Don't drop the pages for imported dmabuf, as they are not
  450. * ours, just free the array we allocated:
  451. */
  452. if (msm_obj->pages)
  453. drm_free_large(msm_obj->pages);
  454. drm_prime_gem_destroy(obj, msm_obj->sgt);
  455. } else {
  456. vunmap(msm_obj->vaddr);
  457. put_pages(obj);
  458. }
  459. if (msm_obj->resv == &msm_obj->_resv)
  460. reservation_object_fini(msm_obj->resv);
  461. drm_gem_object_release(obj);
  462. kfree(msm_obj);
  463. }
  464. /* convenience method to construct a GEM buffer object, and userspace handle */
  465. int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
  466. uint32_t size, uint32_t flags, uint32_t *handle)
  467. {
  468. struct drm_gem_object *obj;
  469. int ret;
  470. ret = mutex_lock_interruptible(&dev->struct_mutex);
  471. if (ret)
  472. return ret;
  473. obj = msm_gem_new(dev, size, flags);
  474. mutex_unlock(&dev->struct_mutex);
  475. if (IS_ERR(obj))
  476. return PTR_ERR(obj);
  477. ret = drm_gem_handle_create(file, obj, handle);
  478. /* drop reference from allocate - handle holds it now */
  479. drm_gem_object_unreference_unlocked(obj);
  480. return ret;
  481. }
  482. static int msm_gem_new_impl(struct drm_device *dev,
  483. uint32_t size, uint32_t flags,
  484. struct drm_gem_object **obj)
  485. {
  486. struct msm_drm_private *priv = dev->dev_private;
  487. struct msm_gem_object *msm_obj;
  488. unsigned sz;
  489. bool use_vram = false;
  490. switch (flags & MSM_BO_CACHE_MASK) {
  491. case MSM_BO_UNCACHED:
  492. case MSM_BO_CACHED:
  493. case MSM_BO_WC:
  494. break;
  495. default:
  496. dev_err(dev->dev, "invalid cache flag: %x\n",
  497. (flags & MSM_BO_CACHE_MASK));
  498. return -EINVAL;
  499. }
  500. if (!iommu_present(&platform_bus_type))
  501. use_vram = true;
  502. else if ((flags & MSM_BO_STOLEN) && priv->vram.size)
  503. use_vram = true;
  504. if (WARN_ON(use_vram && !priv->vram.size))
  505. return -EINVAL;
  506. sz = sizeof(*msm_obj);
  507. if (use_vram)
  508. sz += sizeof(struct drm_mm_node);
  509. msm_obj = kzalloc(sz, GFP_KERNEL);
  510. if (!msm_obj)
  511. return -ENOMEM;
  512. if (use_vram)
  513. msm_obj->vram_node = (void *)&msm_obj[1];
  514. msm_obj->flags = flags;
  515. msm_obj->resv = &msm_obj->_resv;
  516. reservation_object_init(msm_obj->resv);
  517. INIT_LIST_HEAD(&msm_obj->submit_entry);
  518. list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
  519. *obj = &msm_obj->base;
  520. return 0;
  521. }
  522. struct drm_gem_object *msm_gem_new(struct drm_device *dev,
  523. uint32_t size, uint32_t flags)
  524. {
  525. struct drm_gem_object *obj = NULL;
  526. int ret;
  527. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  528. size = PAGE_ALIGN(size);
  529. ret = msm_gem_new_impl(dev, size, flags, &obj);
  530. if (ret)
  531. goto fail;
  532. if (use_pages(obj)) {
  533. ret = drm_gem_object_init(dev, obj, size);
  534. if (ret)
  535. goto fail;
  536. } else {
  537. drm_gem_private_object_init(dev, obj, size);
  538. }
  539. return obj;
  540. fail:
  541. if (obj)
  542. drm_gem_object_unreference(obj);
  543. return ERR_PTR(ret);
  544. }
  545. struct drm_gem_object *msm_gem_import(struct drm_device *dev,
  546. uint32_t size, struct sg_table *sgt)
  547. {
  548. struct msm_gem_object *msm_obj;
  549. struct drm_gem_object *obj;
  550. int ret, npages;
  551. /* if we don't have IOMMU, don't bother pretending we can import: */
  552. if (!iommu_present(&platform_bus_type)) {
  553. dev_err(dev->dev, "cannot import without IOMMU\n");
  554. return ERR_PTR(-EINVAL);
  555. }
  556. size = PAGE_ALIGN(size);
  557. ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
  558. if (ret)
  559. goto fail;
  560. drm_gem_private_object_init(dev, obj, size);
  561. npages = size / PAGE_SIZE;
  562. msm_obj = to_msm_bo(obj);
  563. msm_obj->sgt = sgt;
  564. msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
  565. if (!msm_obj->pages) {
  566. ret = -ENOMEM;
  567. goto fail;
  568. }
  569. ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
  570. if (ret)
  571. goto fail;
  572. return obj;
  573. fail:
  574. if (obj)
  575. drm_gem_object_unreference_unlocked(obj);
  576. return ERR_PTR(ret);
  577. }