omap_gem.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_gem.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob.clark@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/shmem_fs.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/pfn_t.h>
  22. #include <drm/drm_vma_manager.h>
  23. #include "omap_drv.h"
  24. #include "omap_dmm_tiler.h"
  25. /*
  26. * GEM buffer object implementation.
  27. */
  28. /* note: we use upper 8 bits of flags for driver-internal flags: */
  29. #define OMAP_BO_DMA 0x01000000 /* actually is physically contiguous */
  30. #define OMAP_BO_EXT_SYNC 0x02000000 /* externally allocated sync object */
  31. #define OMAP_BO_EXT_MEM 0x04000000 /* externally allocated memory */
  32. struct omap_gem_object {
  33. struct drm_gem_object base;
  34. struct list_head mm_list;
  35. uint32_t flags;
  36. /** width/height for tiled formats (rounded up to slot boundaries) */
  37. uint16_t width, height;
  38. /** roll applied when mapping to DMM */
  39. uint32_t roll;
  40. /**
  41. * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
  42. * is set and the paddr is valid. Also if the buffer is remapped in
  43. * TILER and paddr_cnt > 0, then paddr is valid. But if you are using
  44. * the physical address and OMAP_BO_DMA is not set, then you should
  45. * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
  46. * not removed from under your feet.
  47. *
  48. * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
  49. * buffer is requested, but doesn't mean that it is. Use the
  50. * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
  51. * physical address.
  52. */
  53. dma_addr_t paddr;
  54. /**
  55. * # of users of paddr
  56. */
  57. uint32_t paddr_cnt;
  58. /**
  59. * tiler block used when buffer is remapped in DMM/TILER.
  60. */
  61. struct tiler_block *block;
  62. /**
  63. * Array of backing pages, if allocated. Note that pages are never
  64. * allocated for buffers originally allocated from contiguous memory
  65. */
  66. struct page **pages;
  67. /** addresses corresponding to pages in above array */
  68. dma_addr_t *addrs;
  69. /**
  70. * Virtual address, if mapped.
  71. */
  72. void *vaddr;
  73. /**
  74. * sync-object allocated on demand (if needed)
  75. *
  76. * Per-buffer sync-object for tracking pending and completed hw/dma
  77. * read and write operations. The layout in memory is dictated by
  78. * the SGX firmware, which uses this information to stall the command
  79. * stream if a surface is not ready yet.
  80. *
  81. * Note that when buffer is used by SGX, the sync-object needs to be
  82. * allocated from a special heap of sync-objects. This way many sync
  83. * objects can be packed in a page, and not waste GPU virtual address
  84. * space. Because of this we have to have a omap_gem_set_sync_object()
  85. * API to allow replacement of the syncobj after it has (potentially)
  86. * already been allocated. A bit ugly but I haven't thought of a
  87. * better alternative.
  88. */
  89. struct {
  90. uint32_t write_pending;
  91. uint32_t write_complete;
  92. uint32_t read_pending;
  93. uint32_t read_complete;
  94. } *sync;
  95. };
  96. #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
  97. /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
  98. * not necessarily pinned in TILER all the time, and (b) when they are
  99. * they are not necessarily page aligned, we reserve one or more small
  100. * regions in each of the 2d containers to use as a user-GART where we
  101. * can create a second page-aligned mapping of parts of the buffer
  102. * being accessed from userspace.
  103. *
  104. * Note that we could optimize slightly when we know that multiple
  105. * tiler containers are backed by the same PAT.. but I'll leave that
  106. * for later..
  107. */
  108. #define NUM_USERGART_ENTRIES 2
  109. struct omap_drm_usergart_entry {
  110. struct tiler_block *block; /* the reserved tiler block */
  111. dma_addr_t paddr;
  112. struct drm_gem_object *obj; /* the current pinned obj */
  113. pgoff_t obj_pgoff; /* page offset of obj currently
  114. mapped in */
  115. };
  116. struct omap_drm_usergart {
  117. struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
  118. int height; /* height in rows */
  119. int height_shift; /* ilog2(height in rows) */
  120. int slot_shift; /* ilog2(width per slot) */
  121. int stride_pfn; /* stride in pages */
  122. int last; /* index of last used entry */
  123. };
  124. /* -----------------------------------------------------------------------------
  125. * Helpers
  126. */
  127. /** get mmap offset */
  128. static uint64_t mmap_offset(struct drm_gem_object *obj)
  129. {
  130. struct drm_device *dev = obj->dev;
  131. int ret;
  132. size_t size;
  133. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  134. /* Make it mmapable */
  135. size = omap_gem_mmap_size(obj);
  136. ret = drm_gem_create_mmap_offset_size(obj, size);
  137. if (ret) {
  138. dev_err(dev->dev, "could not allocate mmap offset\n");
  139. return 0;
  140. }
  141. return drm_vma_node_offset_addr(&obj->vma_node);
  142. }
  143. /* GEM objects can either be allocated from contiguous memory (in which
  144. * case obj->filp==NULL), or w/ shmem backing (obj->filp!=NULL). But non
  145. * contiguous buffers can be remapped in TILER/DMM if they need to be
  146. * contiguous... but we don't do this all the time to reduce pressure
  147. * on TILER/DMM space when we know at allocation time that the buffer
  148. * will need to be scanned out.
  149. */
  150. static inline bool is_shmem(struct drm_gem_object *obj)
  151. {
  152. return obj->filp != NULL;
  153. }
  154. /* -----------------------------------------------------------------------------
  155. * Eviction
  156. */
  157. static void evict_entry(struct drm_gem_object *obj,
  158. enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
  159. {
  160. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  161. struct omap_drm_private *priv = obj->dev->dev_private;
  162. int n = priv->usergart[fmt].height;
  163. size_t size = PAGE_SIZE * n;
  164. loff_t off = mmap_offset(obj) +
  165. (entry->obj_pgoff << PAGE_SHIFT);
  166. const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
  167. if (m > 1) {
  168. int i;
  169. /* if stride > than PAGE_SIZE then sparse mapping: */
  170. for (i = n; i > 0; i--) {
  171. unmap_mapping_range(obj->dev->anon_inode->i_mapping,
  172. off, PAGE_SIZE, 1);
  173. off += PAGE_SIZE * m;
  174. }
  175. } else {
  176. unmap_mapping_range(obj->dev->anon_inode->i_mapping,
  177. off, size, 1);
  178. }
  179. entry->obj = NULL;
  180. }
  181. /* Evict a buffer from usergart, if it is mapped there */
  182. static void evict(struct drm_gem_object *obj)
  183. {
  184. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  185. struct omap_drm_private *priv = obj->dev->dev_private;
  186. if (omap_obj->flags & OMAP_BO_TILED) {
  187. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  188. int i;
  189. for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
  190. struct omap_drm_usergart_entry *entry =
  191. &priv->usergart[fmt].entry[i];
  192. if (entry->obj == obj)
  193. evict_entry(obj, fmt, entry);
  194. }
  195. }
  196. }
  197. /* -----------------------------------------------------------------------------
  198. * Page Management
  199. */
  200. /** ensure backing pages are allocated */
  201. static int omap_gem_attach_pages(struct drm_gem_object *obj)
  202. {
  203. struct drm_device *dev = obj->dev;
  204. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  205. struct page **pages;
  206. int npages = obj->size >> PAGE_SHIFT;
  207. int i, ret;
  208. dma_addr_t *addrs;
  209. WARN_ON(omap_obj->pages);
  210. pages = drm_gem_get_pages(obj);
  211. if (IS_ERR(pages)) {
  212. dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
  213. return PTR_ERR(pages);
  214. }
  215. /* for non-cached buffers, ensure the new pages are clean because
  216. * DSS, GPU, etc. are not cache coherent:
  217. */
  218. if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
  219. addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
  220. if (!addrs) {
  221. ret = -ENOMEM;
  222. goto free_pages;
  223. }
  224. for (i = 0; i < npages; i++) {
  225. addrs[i] = dma_map_page(dev->dev, pages[i],
  226. 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
  227. if (dma_mapping_error(dev->dev, addrs[i])) {
  228. dev_warn(dev->dev,
  229. "%s: failed to map page\n", __func__);
  230. for (i = i - 1; i >= 0; --i) {
  231. dma_unmap_page(dev->dev, addrs[i],
  232. PAGE_SIZE, DMA_BIDIRECTIONAL);
  233. }
  234. ret = -ENOMEM;
  235. goto free_addrs;
  236. }
  237. }
  238. } else {
  239. addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
  240. if (!addrs) {
  241. ret = -ENOMEM;
  242. goto free_pages;
  243. }
  244. }
  245. omap_obj->addrs = addrs;
  246. omap_obj->pages = pages;
  247. return 0;
  248. free_addrs:
  249. kfree(addrs);
  250. free_pages:
  251. drm_gem_put_pages(obj, pages, true, false);
  252. return ret;
  253. }
  254. /* acquire pages when needed (for example, for DMA where physically
  255. * contiguous buffer is not required
  256. */
  257. static int get_pages(struct drm_gem_object *obj, struct page ***pages)
  258. {
  259. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  260. int ret = 0;
  261. if (is_shmem(obj) && !omap_obj->pages) {
  262. ret = omap_gem_attach_pages(obj);
  263. if (ret) {
  264. dev_err(obj->dev->dev, "could not attach pages\n");
  265. return ret;
  266. }
  267. }
  268. /* TODO: even phys-contig.. we should have a list of pages? */
  269. *pages = omap_obj->pages;
  270. return 0;
  271. }
  272. /** release backing pages */
  273. static void omap_gem_detach_pages(struct drm_gem_object *obj)
  274. {
  275. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  276. /* for non-cached buffers, ensure the new pages are clean because
  277. * DSS, GPU, etc. are not cache coherent:
  278. */
  279. if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
  280. int i, npages = obj->size >> PAGE_SHIFT;
  281. for (i = 0; i < npages; i++) {
  282. dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
  283. PAGE_SIZE, DMA_BIDIRECTIONAL);
  284. }
  285. }
  286. kfree(omap_obj->addrs);
  287. omap_obj->addrs = NULL;
  288. drm_gem_put_pages(obj, omap_obj->pages, true, false);
  289. omap_obj->pages = NULL;
  290. }
  291. /* get buffer flags */
  292. uint32_t omap_gem_flags(struct drm_gem_object *obj)
  293. {
  294. return to_omap_bo(obj)->flags;
  295. }
  296. uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
  297. {
  298. uint64_t offset;
  299. mutex_lock(&obj->dev->struct_mutex);
  300. offset = mmap_offset(obj);
  301. mutex_unlock(&obj->dev->struct_mutex);
  302. return offset;
  303. }
  304. /** get mmap size */
  305. size_t omap_gem_mmap_size(struct drm_gem_object *obj)
  306. {
  307. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  308. size_t size = obj->size;
  309. if (omap_obj->flags & OMAP_BO_TILED) {
  310. /* for tiled buffers, the virtual size has stride rounded up
  311. * to 4kb.. (to hide the fact that row n+1 might start 16kb or
  312. * 32kb later!). But we don't back the entire buffer with
  313. * pages, only the valid picture part.. so need to adjust for
  314. * this in the size used to mmap and generate mmap offset
  315. */
  316. size = tiler_vsize(gem2fmt(omap_obj->flags),
  317. omap_obj->width, omap_obj->height);
  318. }
  319. return size;
  320. }
  321. /* get tiled size, returns -EINVAL if not tiled buffer */
  322. int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h)
  323. {
  324. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  325. if (omap_obj->flags & OMAP_BO_TILED) {
  326. *w = omap_obj->width;
  327. *h = omap_obj->height;
  328. return 0;
  329. }
  330. return -EINVAL;
  331. }
  332. /* -----------------------------------------------------------------------------
  333. * Fault Handling
  334. */
  335. /* Normal handling for the case of faulting in non-tiled buffers */
  336. static int fault_1d(struct drm_gem_object *obj,
  337. struct vm_area_struct *vma, struct vm_fault *vmf)
  338. {
  339. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  340. unsigned long pfn;
  341. pgoff_t pgoff;
  342. /* We don't use vmf->pgoff since that has the fake offset: */
  343. pgoff = ((unsigned long)vmf->virtual_address -
  344. vma->vm_start) >> PAGE_SHIFT;
  345. if (omap_obj->pages) {
  346. omap_gem_cpu_sync(obj, pgoff);
  347. pfn = page_to_pfn(omap_obj->pages[pgoff]);
  348. } else {
  349. BUG_ON(!(omap_obj->flags & OMAP_BO_DMA));
  350. pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
  351. }
  352. VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
  353. pfn, pfn << PAGE_SHIFT);
  354. return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
  355. __pfn_to_pfn_t(pfn, PFN_DEV));
  356. }
  357. /* Special handling for the case of faulting in 2d tiled buffers */
  358. static int fault_2d(struct drm_gem_object *obj,
  359. struct vm_area_struct *vma, struct vm_fault *vmf)
  360. {
  361. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  362. struct omap_drm_private *priv = obj->dev->dev_private;
  363. struct omap_drm_usergart_entry *entry;
  364. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  365. struct page *pages[64]; /* XXX is this too much to have on stack? */
  366. unsigned long pfn;
  367. pgoff_t pgoff, base_pgoff;
  368. void __user *vaddr;
  369. int i, ret, slots;
  370. /*
  371. * Note the height of the slot is also equal to the number of pages
  372. * that need to be mapped in to fill 4kb wide CPU page. If the slot
  373. * height is 64, then 64 pages fill a 4kb wide by 64 row region.
  374. */
  375. const int n = priv->usergart[fmt].height;
  376. const int n_shift = priv->usergart[fmt].height_shift;
  377. /*
  378. * If buffer width in bytes > PAGE_SIZE then the virtual stride is
  379. * rounded up to next multiple of PAGE_SIZE.. this need to be taken
  380. * into account in some of the math, so figure out virtual stride
  381. * in pages
  382. */
  383. const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
  384. /* We don't use vmf->pgoff since that has the fake offset: */
  385. pgoff = ((unsigned long)vmf->virtual_address -
  386. vma->vm_start) >> PAGE_SHIFT;
  387. /*
  388. * Actual address we start mapping at is rounded down to previous slot
  389. * boundary in the y direction:
  390. */
  391. base_pgoff = round_down(pgoff, m << n_shift);
  392. /* figure out buffer width in slots */
  393. slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
  394. vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
  395. entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
  396. /* evict previous buffer using this usergart entry, if any: */
  397. if (entry->obj)
  398. evict_entry(entry->obj, fmt, entry);
  399. entry->obj = obj;
  400. entry->obj_pgoff = base_pgoff;
  401. /* now convert base_pgoff to phys offset from virt offset: */
  402. base_pgoff = (base_pgoff >> n_shift) * slots;
  403. /* for wider-than 4k.. figure out which part of the slot-row we want: */
  404. if (m > 1) {
  405. int off = pgoff % m;
  406. entry->obj_pgoff += off;
  407. base_pgoff /= m;
  408. slots = min(slots - (off << n_shift), n);
  409. base_pgoff += off << n_shift;
  410. vaddr += off << PAGE_SHIFT;
  411. }
  412. /*
  413. * Map in pages. Beyond the valid pixel part of the buffer, we set
  414. * pages[i] to NULL to get a dummy page mapped in.. if someone
  415. * reads/writes it they will get random/undefined content, but at
  416. * least it won't be corrupting whatever other random page used to
  417. * be mapped in, or other undefined behavior.
  418. */
  419. memcpy(pages, &omap_obj->pages[base_pgoff],
  420. sizeof(struct page *) * slots);
  421. memset(pages + slots, 0,
  422. sizeof(struct page *) * (n - slots));
  423. ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
  424. if (ret) {
  425. dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
  426. return ret;
  427. }
  428. pfn = entry->paddr >> PAGE_SHIFT;
  429. VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
  430. pfn, pfn << PAGE_SHIFT);
  431. for (i = n; i > 0; i--) {
  432. vm_insert_mixed(vma, (unsigned long)vaddr,
  433. __pfn_to_pfn_t(pfn, PFN_DEV));
  434. pfn += priv->usergart[fmt].stride_pfn;
  435. vaddr += PAGE_SIZE * m;
  436. }
  437. /* simple round-robin: */
  438. priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
  439. % NUM_USERGART_ENTRIES;
  440. return 0;
  441. }
  442. /**
  443. * omap_gem_fault - pagefault handler for GEM objects
  444. * @vma: the VMA of the GEM object
  445. * @vmf: fault detail
  446. *
  447. * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
  448. * does most of the work for us including the actual map/unmap calls
  449. * but we need to do the actual page work.
  450. *
  451. * The VMA was set up by GEM. In doing so it also ensured that the
  452. * vma->vm_private_data points to the GEM object that is backing this
  453. * mapping.
  454. */
  455. int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  456. {
  457. struct drm_gem_object *obj = vma->vm_private_data;
  458. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  459. struct drm_device *dev = obj->dev;
  460. struct page **pages;
  461. int ret;
  462. /* Make sure we don't parallel update on a fault, nor move or remove
  463. * something from beneath our feet
  464. */
  465. mutex_lock(&dev->struct_mutex);
  466. /* if a shmem backed object, make sure we have pages attached now */
  467. ret = get_pages(obj, &pages);
  468. if (ret)
  469. goto fail;
  470. /* where should we do corresponding put_pages().. we are mapping
  471. * the original page, rather than thru a GART, so we can't rely
  472. * on eviction to trigger this. But munmap() or all mappings should
  473. * probably trigger put_pages()?
  474. */
  475. if (omap_obj->flags & OMAP_BO_TILED)
  476. ret = fault_2d(obj, vma, vmf);
  477. else
  478. ret = fault_1d(obj, vma, vmf);
  479. fail:
  480. mutex_unlock(&dev->struct_mutex);
  481. switch (ret) {
  482. case 0:
  483. case -ERESTARTSYS:
  484. case -EINTR:
  485. return VM_FAULT_NOPAGE;
  486. case -ENOMEM:
  487. return VM_FAULT_OOM;
  488. default:
  489. return VM_FAULT_SIGBUS;
  490. }
  491. }
  492. /** We override mainly to fix up some of the vm mapping flags.. */
  493. int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  494. {
  495. int ret;
  496. ret = drm_gem_mmap(filp, vma);
  497. if (ret) {
  498. DBG("mmap failed: %d", ret);
  499. return ret;
  500. }
  501. return omap_gem_mmap_obj(vma->vm_private_data, vma);
  502. }
  503. int omap_gem_mmap_obj(struct drm_gem_object *obj,
  504. struct vm_area_struct *vma)
  505. {
  506. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  507. vma->vm_flags &= ~VM_PFNMAP;
  508. vma->vm_flags |= VM_MIXEDMAP;
  509. if (omap_obj->flags & OMAP_BO_WC) {
  510. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  511. } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
  512. vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
  513. } else {
  514. /*
  515. * We do have some private objects, at least for scanout buffers
  516. * on hardware without DMM/TILER. But these are allocated write-
  517. * combine
  518. */
  519. if (WARN_ON(!obj->filp))
  520. return -EINVAL;
  521. /*
  522. * Shunt off cached objs to shmem file so they have their own
  523. * address_space (so unmap_mapping_range does what we want,
  524. * in particular in the case of mmap'd dmabufs)
  525. */
  526. fput(vma->vm_file);
  527. vma->vm_pgoff = 0;
  528. vma->vm_file = get_file(obj->filp);
  529. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  530. }
  531. return 0;
  532. }
  533. /* -----------------------------------------------------------------------------
  534. * Dumb Buffers
  535. */
  536. /**
  537. * omap_gem_dumb_create - create a dumb buffer
  538. * @drm_file: our client file
  539. * @dev: our device
  540. * @args: the requested arguments copied from userspace
  541. *
  542. * Allocate a buffer suitable for use for a frame buffer of the
  543. * form described by user space. Give userspace a handle by which
  544. * to reference it.
  545. */
  546. int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  547. struct drm_mode_create_dumb *args)
  548. {
  549. union omap_gem_size gsize;
  550. args->pitch = align_pitch(0, args->width, args->bpp);
  551. args->size = PAGE_ALIGN(args->pitch * args->height);
  552. gsize = (union omap_gem_size){
  553. .bytes = args->size,
  554. };
  555. return omap_gem_new_handle(dev, file, gsize,
  556. OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
  557. }
  558. /**
  559. * omap_gem_dumb_map - buffer mapping for dumb interface
  560. * @file: our drm client file
  561. * @dev: drm device
  562. * @handle: GEM handle to the object (from dumb_create)
  563. *
  564. * Do the necessary setup to allow the mapping of the frame buffer
  565. * into user memory. We don't have to do much here at the moment.
  566. */
  567. int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  568. uint32_t handle, uint64_t *offset)
  569. {
  570. struct drm_gem_object *obj;
  571. int ret = 0;
  572. /* GEM does all our handle to object mapping */
  573. obj = drm_gem_object_lookup(dev, file, handle);
  574. if (obj == NULL) {
  575. ret = -ENOENT;
  576. goto fail;
  577. }
  578. *offset = omap_gem_mmap_offset(obj);
  579. drm_gem_object_unreference_unlocked(obj);
  580. fail:
  581. return ret;
  582. }
  583. #ifdef CONFIG_DRM_FBDEV_EMULATION
  584. /* Set scrolling position. This allows us to implement fast scrolling
  585. * for console.
  586. *
  587. * Call only from non-atomic contexts.
  588. */
  589. int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
  590. {
  591. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  592. uint32_t npages = obj->size >> PAGE_SHIFT;
  593. int ret = 0;
  594. if (roll > npages) {
  595. dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
  596. return -EINVAL;
  597. }
  598. omap_obj->roll = roll;
  599. mutex_lock(&obj->dev->struct_mutex);
  600. /* if we aren't mapped yet, we don't need to do anything */
  601. if (omap_obj->block) {
  602. struct page **pages;
  603. ret = get_pages(obj, &pages);
  604. if (ret)
  605. goto fail;
  606. ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
  607. if (ret)
  608. dev_err(obj->dev->dev, "could not repin: %d\n", ret);
  609. }
  610. fail:
  611. mutex_unlock(&obj->dev->struct_mutex);
  612. return ret;
  613. }
  614. #endif
  615. /* -----------------------------------------------------------------------------
  616. * Memory Management & DMA Sync
  617. */
  618. /**
  619. * shmem buffers that are mapped cached can simulate coherency via using
  620. * page faulting to keep track of dirty pages
  621. */
  622. static inline bool is_cached_coherent(struct drm_gem_object *obj)
  623. {
  624. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  625. return is_shmem(obj) &&
  626. ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED);
  627. }
  628. /* Sync the buffer for CPU access.. note pages should already be
  629. * attached, ie. omap_gem_get_pages()
  630. */
  631. void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
  632. {
  633. struct drm_device *dev = obj->dev;
  634. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  635. if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
  636. dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
  637. PAGE_SIZE, DMA_BIDIRECTIONAL);
  638. omap_obj->addrs[pgoff] = 0;
  639. }
  640. }
  641. /* sync the buffer for DMA access */
  642. void omap_gem_dma_sync(struct drm_gem_object *obj,
  643. enum dma_data_direction dir)
  644. {
  645. struct drm_device *dev = obj->dev;
  646. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  647. if (is_cached_coherent(obj)) {
  648. int i, npages = obj->size >> PAGE_SHIFT;
  649. struct page **pages = omap_obj->pages;
  650. bool dirty = false;
  651. for (i = 0; i < npages; i++) {
  652. if (!omap_obj->addrs[i]) {
  653. omap_obj->addrs[i] = dma_map_page(dev->dev, pages[i], 0,
  654. PAGE_SIZE, DMA_BIDIRECTIONAL);
  655. dirty = true;
  656. }
  657. }
  658. if (dirty) {
  659. unmap_mapping_range(obj->filp->f_mapping, 0,
  660. omap_gem_mmap_size(obj), 1);
  661. }
  662. }
  663. }
  664. /* Get physical address for DMA.. if 'remap' is true, and the buffer is not
  665. * already contiguous, remap it to pin in physically contiguous memory.. (ie.
  666. * map in TILER)
  667. */
  668. int omap_gem_get_paddr(struct drm_gem_object *obj,
  669. dma_addr_t *paddr, bool remap)
  670. {
  671. struct omap_drm_private *priv = obj->dev->dev_private;
  672. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  673. int ret = 0;
  674. mutex_lock(&obj->dev->struct_mutex);
  675. if (remap && is_shmem(obj) && priv->has_dmm) {
  676. if (omap_obj->paddr_cnt == 0) {
  677. struct page **pages;
  678. uint32_t npages = obj->size >> PAGE_SHIFT;
  679. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  680. struct tiler_block *block;
  681. BUG_ON(omap_obj->block);
  682. ret = get_pages(obj, &pages);
  683. if (ret)
  684. goto fail;
  685. if (omap_obj->flags & OMAP_BO_TILED) {
  686. block = tiler_reserve_2d(fmt,
  687. omap_obj->width,
  688. omap_obj->height, 0);
  689. } else {
  690. block = tiler_reserve_1d(obj->size);
  691. }
  692. if (IS_ERR(block)) {
  693. ret = PTR_ERR(block);
  694. dev_err(obj->dev->dev,
  695. "could not remap: %d (%d)\n", ret, fmt);
  696. goto fail;
  697. }
  698. /* TODO: enable async refill.. */
  699. ret = tiler_pin(block, pages, npages,
  700. omap_obj->roll, true);
  701. if (ret) {
  702. tiler_release(block);
  703. dev_err(obj->dev->dev,
  704. "could not pin: %d\n", ret);
  705. goto fail;
  706. }
  707. omap_obj->paddr = tiler_ssptr(block);
  708. omap_obj->block = block;
  709. DBG("got paddr: %pad", &omap_obj->paddr);
  710. }
  711. omap_obj->paddr_cnt++;
  712. *paddr = omap_obj->paddr;
  713. } else if (omap_obj->flags & OMAP_BO_DMA) {
  714. *paddr = omap_obj->paddr;
  715. } else {
  716. ret = -EINVAL;
  717. goto fail;
  718. }
  719. fail:
  720. mutex_unlock(&obj->dev->struct_mutex);
  721. return ret;
  722. }
  723. /* Release physical address, when DMA is no longer being performed.. this
  724. * could potentially unpin and unmap buffers from TILER
  725. */
  726. void omap_gem_put_paddr(struct drm_gem_object *obj)
  727. {
  728. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  729. int ret;
  730. mutex_lock(&obj->dev->struct_mutex);
  731. if (omap_obj->paddr_cnt > 0) {
  732. omap_obj->paddr_cnt--;
  733. if (omap_obj->paddr_cnt == 0) {
  734. ret = tiler_unpin(omap_obj->block);
  735. if (ret) {
  736. dev_err(obj->dev->dev,
  737. "could not unpin pages: %d\n", ret);
  738. }
  739. ret = tiler_release(omap_obj->block);
  740. if (ret) {
  741. dev_err(obj->dev->dev,
  742. "could not release unmap: %d\n", ret);
  743. }
  744. omap_obj->paddr = 0;
  745. omap_obj->block = NULL;
  746. }
  747. }
  748. mutex_unlock(&obj->dev->struct_mutex);
  749. }
  750. /* Get rotated scanout address (only valid if already pinned), at the
  751. * specified orientation and x,y offset from top-left corner of buffer
  752. * (only valid for tiled 2d buffers)
  753. */
  754. int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
  755. int x, int y, dma_addr_t *paddr)
  756. {
  757. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  758. int ret = -EINVAL;
  759. mutex_lock(&obj->dev->struct_mutex);
  760. if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
  761. (omap_obj->flags & OMAP_BO_TILED)) {
  762. *paddr = tiler_tsptr(omap_obj->block, orient, x, y);
  763. ret = 0;
  764. }
  765. mutex_unlock(&obj->dev->struct_mutex);
  766. return ret;
  767. }
  768. /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
  769. int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
  770. {
  771. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  772. int ret = -EINVAL;
  773. if (omap_obj->flags & OMAP_BO_TILED)
  774. ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
  775. return ret;
  776. }
  777. /* if !remap, and we don't have pages backing, then fail, rather than
  778. * increasing the pin count (which we don't really do yet anyways,
  779. * because we don't support swapping pages back out). And 'remap'
  780. * might not be quite the right name, but I wanted to keep it working
  781. * similarly to omap_gem_get_paddr(). Note though that mutex is not
  782. * aquired if !remap (because this can be called in atomic ctxt),
  783. * but probably omap_gem_get_paddr() should be changed to work in the
  784. * same way. If !remap, a matching omap_gem_put_pages() call is not
  785. * required (and should not be made).
  786. */
  787. int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
  788. bool remap)
  789. {
  790. int ret;
  791. if (!remap) {
  792. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  793. if (!omap_obj->pages)
  794. return -ENOMEM;
  795. *pages = omap_obj->pages;
  796. return 0;
  797. }
  798. mutex_lock(&obj->dev->struct_mutex);
  799. ret = get_pages(obj, pages);
  800. mutex_unlock(&obj->dev->struct_mutex);
  801. return ret;
  802. }
  803. /* release pages when DMA no longer being performed */
  804. int omap_gem_put_pages(struct drm_gem_object *obj)
  805. {
  806. /* do something here if we dynamically attach/detach pages.. at
  807. * least they would no longer need to be pinned if everyone has
  808. * released the pages..
  809. */
  810. return 0;
  811. }
  812. #ifdef CONFIG_DRM_FBDEV_EMULATION
  813. /* Get kernel virtual address for CPU access.. this more or less only
  814. * exists for omap_fbdev. This should be called with struct_mutex
  815. * held.
  816. */
  817. void *omap_gem_vaddr(struct drm_gem_object *obj)
  818. {
  819. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  820. WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
  821. if (!omap_obj->vaddr) {
  822. struct page **pages;
  823. int ret = get_pages(obj, &pages);
  824. if (ret)
  825. return ERR_PTR(ret);
  826. omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
  827. VM_MAP, pgprot_writecombine(PAGE_KERNEL));
  828. }
  829. return omap_obj->vaddr;
  830. }
  831. #endif
  832. /* -----------------------------------------------------------------------------
  833. * Power Management
  834. */
  835. #ifdef CONFIG_PM
  836. /* re-pin objects in DMM in resume path: */
  837. int omap_gem_resume(struct device *dev)
  838. {
  839. struct drm_device *drm_dev = dev_get_drvdata(dev);
  840. struct omap_drm_private *priv = drm_dev->dev_private;
  841. struct omap_gem_object *omap_obj;
  842. int ret = 0;
  843. list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
  844. if (omap_obj->block) {
  845. struct drm_gem_object *obj = &omap_obj->base;
  846. uint32_t npages = obj->size >> PAGE_SHIFT;
  847. WARN_ON(!omap_obj->pages); /* this can't happen */
  848. ret = tiler_pin(omap_obj->block,
  849. omap_obj->pages, npages,
  850. omap_obj->roll, true);
  851. if (ret) {
  852. dev_err(dev, "could not repin: %d\n", ret);
  853. return ret;
  854. }
  855. }
  856. }
  857. return 0;
  858. }
  859. #endif
  860. /* -----------------------------------------------------------------------------
  861. * DebugFS
  862. */
  863. #ifdef CONFIG_DEBUG_FS
  864. void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
  865. {
  866. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  867. uint64_t off;
  868. off = drm_vma_node_start(&obj->vma_node);
  869. seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
  870. omap_obj->flags, obj->name, obj->refcount.refcount.counter,
  871. off, &omap_obj->paddr, omap_obj->paddr_cnt,
  872. omap_obj->vaddr, omap_obj->roll);
  873. if (omap_obj->flags & OMAP_BO_TILED) {
  874. seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
  875. if (omap_obj->block) {
  876. struct tcm_area *area = &omap_obj->block->area;
  877. seq_printf(m, " (%dx%d, %dx%d)",
  878. area->p0.x, area->p0.y,
  879. area->p1.x, area->p1.y);
  880. }
  881. } else {
  882. seq_printf(m, " %d", obj->size);
  883. }
  884. seq_printf(m, "\n");
  885. }
  886. void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
  887. {
  888. struct omap_gem_object *omap_obj;
  889. int count = 0;
  890. size_t size = 0;
  891. list_for_each_entry(omap_obj, list, mm_list) {
  892. struct drm_gem_object *obj = &omap_obj->base;
  893. seq_printf(m, " ");
  894. omap_gem_describe(obj, m);
  895. count++;
  896. size += obj->size;
  897. }
  898. seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
  899. }
  900. #endif
  901. /* -----------------------------------------------------------------------------
  902. * Buffer Synchronization
  903. */
  904. static DEFINE_SPINLOCK(sync_lock);
  905. struct omap_gem_sync_waiter {
  906. struct list_head list;
  907. struct omap_gem_object *omap_obj;
  908. enum omap_gem_op op;
  909. uint32_t read_target, write_target;
  910. /* notify called w/ sync_lock held */
  911. void (*notify)(void *arg);
  912. void *arg;
  913. };
  914. /* list of omap_gem_sync_waiter.. the notify fxn gets called back when
  915. * the read and/or write target count is achieved which can call a user
  916. * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
  917. * cpu access), etc.
  918. */
  919. static LIST_HEAD(waiters);
  920. static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
  921. {
  922. struct omap_gem_object *omap_obj = waiter->omap_obj;
  923. if ((waiter->op & OMAP_GEM_READ) &&
  924. (omap_obj->sync->write_complete < waiter->write_target))
  925. return true;
  926. if ((waiter->op & OMAP_GEM_WRITE) &&
  927. (omap_obj->sync->read_complete < waiter->read_target))
  928. return true;
  929. return false;
  930. }
  931. /* macro for sync debug.. */
  932. #define SYNCDBG 0
  933. #define SYNC(fmt, ...) do { if (SYNCDBG) \
  934. printk(KERN_ERR "%s:%d: "fmt"\n", \
  935. __func__, __LINE__, ##__VA_ARGS__); \
  936. } while (0)
  937. static void sync_op_update(void)
  938. {
  939. struct omap_gem_sync_waiter *waiter, *n;
  940. list_for_each_entry_safe(waiter, n, &waiters, list) {
  941. if (!is_waiting(waiter)) {
  942. list_del(&waiter->list);
  943. SYNC("notify: %p", waiter);
  944. waiter->notify(waiter->arg);
  945. kfree(waiter);
  946. }
  947. }
  948. }
  949. static inline int sync_op(struct drm_gem_object *obj,
  950. enum omap_gem_op op, bool start)
  951. {
  952. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  953. int ret = 0;
  954. spin_lock(&sync_lock);
  955. if (!omap_obj->sync) {
  956. omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
  957. if (!omap_obj->sync) {
  958. ret = -ENOMEM;
  959. goto unlock;
  960. }
  961. }
  962. if (start) {
  963. if (op & OMAP_GEM_READ)
  964. omap_obj->sync->read_pending++;
  965. if (op & OMAP_GEM_WRITE)
  966. omap_obj->sync->write_pending++;
  967. } else {
  968. if (op & OMAP_GEM_READ)
  969. omap_obj->sync->read_complete++;
  970. if (op & OMAP_GEM_WRITE)
  971. omap_obj->sync->write_complete++;
  972. sync_op_update();
  973. }
  974. unlock:
  975. spin_unlock(&sync_lock);
  976. return ret;
  977. }
  978. /* it is a bit lame to handle updates in this sort of polling way, but
  979. * in case of PVR, the GPU can directly update read/write complete
  980. * values, and not really tell us which ones it updated.. this also
  981. * means that sync_lock is not quite sufficient. So we'll need to
  982. * do something a bit better when it comes time to add support for
  983. * separate 2d hw..
  984. */
  985. void omap_gem_op_update(void)
  986. {
  987. spin_lock(&sync_lock);
  988. sync_op_update();
  989. spin_unlock(&sync_lock);
  990. }
  991. /* mark the start of read and/or write operation */
  992. int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
  993. {
  994. return sync_op(obj, op, true);
  995. }
  996. int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
  997. {
  998. return sync_op(obj, op, false);
  999. }
  1000. static DECLARE_WAIT_QUEUE_HEAD(sync_event);
  1001. static void sync_notify(void *arg)
  1002. {
  1003. struct task_struct **waiter_task = arg;
  1004. *waiter_task = NULL;
  1005. wake_up_all(&sync_event);
  1006. }
  1007. int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
  1008. {
  1009. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1010. int ret = 0;
  1011. if (omap_obj->sync) {
  1012. struct task_struct *waiter_task = current;
  1013. struct omap_gem_sync_waiter *waiter =
  1014. kzalloc(sizeof(*waiter), GFP_KERNEL);
  1015. if (!waiter)
  1016. return -ENOMEM;
  1017. waiter->omap_obj = omap_obj;
  1018. waiter->op = op;
  1019. waiter->read_target = omap_obj->sync->read_pending;
  1020. waiter->write_target = omap_obj->sync->write_pending;
  1021. waiter->notify = sync_notify;
  1022. waiter->arg = &waiter_task;
  1023. spin_lock(&sync_lock);
  1024. if (is_waiting(waiter)) {
  1025. SYNC("waited: %p", waiter);
  1026. list_add_tail(&waiter->list, &waiters);
  1027. spin_unlock(&sync_lock);
  1028. ret = wait_event_interruptible(sync_event,
  1029. (waiter_task == NULL));
  1030. spin_lock(&sync_lock);
  1031. if (waiter_task) {
  1032. SYNC("interrupted: %p", waiter);
  1033. /* we were interrupted */
  1034. list_del(&waiter->list);
  1035. waiter_task = NULL;
  1036. } else {
  1037. /* freed in sync_op_update() */
  1038. waiter = NULL;
  1039. }
  1040. }
  1041. spin_unlock(&sync_lock);
  1042. kfree(waiter);
  1043. }
  1044. return ret;
  1045. }
  1046. /* call fxn(arg), either synchronously or asynchronously if the op
  1047. * is currently blocked.. fxn() can be called from any context
  1048. *
  1049. * (TODO for now fxn is called back from whichever context calls
  1050. * omap_gem_op_update().. but this could be better defined later
  1051. * if needed)
  1052. *
  1053. * TODO more code in common w/ _sync()..
  1054. */
  1055. int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
  1056. void (*fxn)(void *arg), void *arg)
  1057. {
  1058. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1059. if (omap_obj->sync) {
  1060. struct omap_gem_sync_waiter *waiter =
  1061. kzalloc(sizeof(*waiter), GFP_ATOMIC);
  1062. if (!waiter)
  1063. return -ENOMEM;
  1064. waiter->omap_obj = omap_obj;
  1065. waiter->op = op;
  1066. waiter->read_target = omap_obj->sync->read_pending;
  1067. waiter->write_target = omap_obj->sync->write_pending;
  1068. waiter->notify = fxn;
  1069. waiter->arg = arg;
  1070. spin_lock(&sync_lock);
  1071. if (is_waiting(waiter)) {
  1072. SYNC("waited: %p", waiter);
  1073. list_add_tail(&waiter->list, &waiters);
  1074. spin_unlock(&sync_lock);
  1075. return 0;
  1076. }
  1077. spin_unlock(&sync_lock);
  1078. kfree(waiter);
  1079. }
  1080. /* no waiting.. */
  1081. fxn(arg);
  1082. return 0;
  1083. }
  1084. /* special API so PVR can update the buffer to use a sync-object allocated
  1085. * from it's sync-obj heap. Only used for a newly allocated (from PVR's
  1086. * perspective) sync-object, so we overwrite the new syncobj w/ values
  1087. * from the already allocated syncobj (if there is one)
  1088. */
  1089. int omap_gem_set_sync_object(struct drm_gem_object *obj, void *syncobj)
  1090. {
  1091. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1092. int ret = 0;
  1093. spin_lock(&sync_lock);
  1094. if ((omap_obj->flags & OMAP_BO_EXT_SYNC) && !syncobj) {
  1095. /* clearing a previously set syncobj */
  1096. syncobj = kmemdup(omap_obj->sync, sizeof(*omap_obj->sync),
  1097. GFP_ATOMIC);
  1098. if (!syncobj) {
  1099. ret = -ENOMEM;
  1100. goto unlock;
  1101. }
  1102. omap_obj->flags &= ~OMAP_BO_EXT_SYNC;
  1103. omap_obj->sync = syncobj;
  1104. } else if (syncobj && !(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
  1105. /* replacing an existing syncobj */
  1106. if (omap_obj->sync) {
  1107. memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
  1108. kfree(omap_obj->sync);
  1109. }
  1110. omap_obj->flags |= OMAP_BO_EXT_SYNC;
  1111. omap_obj->sync = syncobj;
  1112. }
  1113. unlock:
  1114. spin_unlock(&sync_lock);
  1115. return ret;
  1116. }
  1117. /* -----------------------------------------------------------------------------
  1118. * Constructor & Destructor
  1119. */
  1120. /* don't call directly.. called from GEM core when it is time to actually
  1121. * free the object..
  1122. */
  1123. void omap_gem_free_object(struct drm_gem_object *obj)
  1124. {
  1125. struct drm_device *dev = obj->dev;
  1126. struct omap_drm_private *priv = dev->dev_private;
  1127. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1128. evict(obj);
  1129. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1130. spin_lock(&priv->list_lock);
  1131. list_del(&omap_obj->mm_list);
  1132. spin_unlock(&priv->list_lock);
  1133. /* this means the object is still pinned.. which really should
  1134. * not happen. I think..
  1135. */
  1136. WARN_ON(omap_obj->paddr_cnt > 0);
  1137. /* don't free externally allocated backing memory */
  1138. if (!(omap_obj->flags & OMAP_BO_EXT_MEM)) {
  1139. if (omap_obj->pages)
  1140. omap_gem_detach_pages(obj);
  1141. if (!is_shmem(obj)) {
  1142. dma_free_writecombine(dev->dev, obj->size,
  1143. omap_obj->vaddr, omap_obj->paddr);
  1144. } else if (omap_obj->vaddr) {
  1145. vunmap(omap_obj->vaddr);
  1146. }
  1147. }
  1148. /* don't free externally allocated syncobj */
  1149. if (!(omap_obj->flags & OMAP_BO_EXT_SYNC))
  1150. kfree(omap_obj->sync);
  1151. drm_gem_object_release(obj);
  1152. kfree(omap_obj);
  1153. }
  1154. /* GEM buffer object constructor */
  1155. struct drm_gem_object *omap_gem_new(struct drm_device *dev,
  1156. union omap_gem_size gsize, uint32_t flags)
  1157. {
  1158. struct omap_drm_private *priv = dev->dev_private;
  1159. struct omap_gem_object *omap_obj;
  1160. struct drm_gem_object *obj;
  1161. struct address_space *mapping;
  1162. size_t size;
  1163. int ret;
  1164. if (flags & OMAP_BO_TILED) {
  1165. if (!priv->usergart) {
  1166. dev_err(dev->dev, "Tiled buffers require DMM\n");
  1167. return NULL;
  1168. }
  1169. /* tiled buffers are always shmem paged backed.. when they are
  1170. * scanned out, they are remapped into DMM/TILER
  1171. */
  1172. flags &= ~OMAP_BO_SCANOUT;
  1173. /* currently don't allow cached buffers.. there is some caching
  1174. * stuff that needs to be handled better
  1175. */
  1176. flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
  1177. flags |= tiler_get_cpu_cache_flags();
  1178. /* align dimensions to slot boundaries... */
  1179. tiler_align(gem2fmt(flags),
  1180. &gsize.tiled.width, &gsize.tiled.height);
  1181. /* ...and calculate size based on aligned dimensions */
  1182. size = tiler_size(gem2fmt(flags),
  1183. gsize.tiled.width, gsize.tiled.height);
  1184. } else {
  1185. size = PAGE_ALIGN(gsize.bytes);
  1186. }
  1187. omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
  1188. if (!omap_obj)
  1189. return NULL;
  1190. obj = &omap_obj->base;
  1191. if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
  1192. /* attempt to allocate contiguous memory if we don't
  1193. * have DMM for remappign discontiguous buffers
  1194. */
  1195. omap_obj->vaddr = dma_alloc_writecombine(dev->dev, size,
  1196. &omap_obj->paddr, GFP_KERNEL);
  1197. if (!omap_obj->vaddr) {
  1198. kfree(omap_obj);
  1199. return NULL;
  1200. }
  1201. flags |= OMAP_BO_DMA;
  1202. }
  1203. spin_lock(&priv->list_lock);
  1204. list_add(&omap_obj->mm_list, &priv->obj_list);
  1205. spin_unlock(&priv->list_lock);
  1206. omap_obj->flags = flags;
  1207. if (flags & OMAP_BO_TILED) {
  1208. omap_obj->width = gsize.tiled.width;
  1209. omap_obj->height = gsize.tiled.height;
  1210. }
  1211. if (flags & (OMAP_BO_DMA|OMAP_BO_EXT_MEM)) {
  1212. drm_gem_private_object_init(dev, obj, size);
  1213. } else {
  1214. ret = drm_gem_object_init(dev, obj, size);
  1215. if (ret)
  1216. goto fail;
  1217. mapping = file_inode(obj->filp)->i_mapping;
  1218. mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
  1219. }
  1220. return obj;
  1221. fail:
  1222. omap_gem_free_object(obj);
  1223. return NULL;
  1224. }
  1225. /* convenience method to construct a GEM buffer object, and userspace handle */
  1226. int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
  1227. union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
  1228. {
  1229. struct drm_gem_object *obj;
  1230. int ret;
  1231. obj = omap_gem_new(dev, gsize, flags);
  1232. if (!obj)
  1233. return -ENOMEM;
  1234. ret = drm_gem_handle_create(file, obj, handle);
  1235. if (ret) {
  1236. omap_gem_free_object(obj);
  1237. return ret;
  1238. }
  1239. /* drop reference from allocate - handle holds it now */
  1240. drm_gem_object_unreference_unlocked(obj);
  1241. return 0;
  1242. }
  1243. /* -----------------------------------------------------------------------------
  1244. * Init & Cleanup
  1245. */
  1246. /* If DMM is used, we need to set some stuff up.. */
  1247. void omap_gem_init(struct drm_device *dev)
  1248. {
  1249. struct omap_drm_private *priv = dev->dev_private;
  1250. struct omap_drm_usergart *usergart;
  1251. const enum tiler_fmt fmts[] = {
  1252. TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
  1253. };
  1254. int i, j;
  1255. if (!dmm_is_available()) {
  1256. /* DMM only supported on OMAP4 and later, so this isn't fatal */
  1257. dev_warn(dev->dev, "DMM not available, disable DMM support\n");
  1258. return;
  1259. }
  1260. usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
  1261. if (!usergart)
  1262. return;
  1263. /* reserve 4k aligned/wide regions for userspace mappings: */
  1264. for (i = 0; i < ARRAY_SIZE(fmts); i++) {
  1265. uint16_t h = 1, w = PAGE_SIZE >> i;
  1266. tiler_align(fmts[i], &w, &h);
  1267. /* note: since each region is 1 4kb page wide, and minimum
  1268. * number of rows, the height ends up being the same as the
  1269. * # of pages in the region
  1270. */
  1271. usergart[i].height = h;
  1272. usergart[i].height_shift = ilog2(h);
  1273. usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
  1274. usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
  1275. for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
  1276. struct omap_drm_usergart_entry *entry;
  1277. struct tiler_block *block;
  1278. entry = &usergart[i].entry[j];
  1279. block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
  1280. if (IS_ERR(block)) {
  1281. dev_err(dev->dev,
  1282. "reserve failed: %d, %d, %ld\n",
  1283. i, j, PTR_ERR(block));
  1284. return;
  1285. }
  1286. entry->paddr = tiler_ssptr(block);
  1287. entry->block = block;
  1288. DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h,
  1289. &entry->paddr,
  1290. usergart[i].stride_pfn << PAGE_SHIFT);
  1291. }
  1292. }
  1293. priv->usergart = usergart;
  1294. priv->has_dmm = true;
  1295. }
  1296. void omap_gem_deinit(struct drm_device *dev)
  1297. {
  1298. struct omap_drm_private *priv = dev->dev_private;
  1299. /* I believe we can rely on there being no more outstanding GEM
  1300. * objects which could depend on usergart/dmm at this point.
  1301. */
  1302. kfree(priv->usergart);
  1303. }