omap_gem.c 40 KB

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