omap_gem.c 40 KB

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