omap_gem.c 40 KB

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