omap_gem.c 40 KB

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