i915_gem.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include <linux/swap.h>
  32. static int
  33. i915_gem_object_set_domain(struct drm_gem_object *obj,
  34. uint32_t read_domains,
  35. uint32_t write_domain);
  36. static int
  37. i915_gem_object_set_domain_range(struct drm_gem_object *obj,
  38. uint64_t offset,
  39. uint64_t size,
  40. uint32_t read_domains,
  41. uint32_t write_domain);
  42. static int
  43. i915_gem_set_domain(struct drm_gem_object *obj,
  44. struct drm_file *file_priv,
  45. uint32_t read_domains,
  46. uint32_t write_domain);
  47. static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
  48. static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
  49. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  50. static void
  51. i915_gem_cleanup_ringbuffer(struct drm_device *dev);
  52. int
  53. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  54. struct drm_file *file_priv)
  55. {
  56. drm_i915_private_t *dev_priv = dev->dev_private;
  57. struct drm_i915_gem_init *args = data;
  58. mutex_lock(&dev->struct_mutex);
  59. if (args->gtt_start >= args->gtt_end ||
  60. (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
  61. (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
  62. mutex_unlock(&dev->struct_mutex);
  63. return -EINVAL;
  64. }
  65. drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
  66. args->gtt_end - args->gtt_start);
  67. dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
  68. mutex_unlock(&dev->struct_mutex);
  69. return 0;
  70. }
  71. int
  72. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  73. struct drm_file *file_priv)
  74. {
  75. drm_i915_private_t *dev_priv = dev->dev_private;
  76. struct drm_i915_gem_get_aperture *args = data;
  77. struct drm_i915_gem_object *obj_priv;
  78. if (!(dev->driver->driver_features & DRIVER_GEM))
  79. return -ENODEV;
  80. args->aper_size = dev->gtt_total;
  81. args->aper_available_size = args->aper_size;
  82. list_for_each_entry(obj_priv, &dev_priv->mm.active_list, list) {
  83. if (obj_priv->pin_count > 0)
  84. args->aper_available_size -= obj_priv->obj->size;
  85. }
  86. return 0;
  87. }
  88. /**
  89. * Creates a new mm object and returns a handle to it.
  90. */
  91. int
  92. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  93. struct drm_file *file_priv)
  94. {
  95. struct drm_i915_gem_create *args = data;
  96. struct drm_gem_object *obj;
  97. int handle, ret;
  98. args->size = roundup(args->size, PAGE_SIZE);
  99. /* Allocate the new object */
  100. obj = drm_gem_object_alloc(dev, args->size);
  101. if (obj == NULL)
  102. return -ENOMEM;
  103. ret = drm_gem_handle_create(file_priv, obj, &handle);
  104. mutex_lock(&dev->struct_mutex);
  105. drm_gem_object_handle_unreference(obj);
  106. mutex_unlock(&dev->struct_mutex);
  107. if (ret)
  108. return ret;
  109. args->handle = handle;
  110. return 0;
  111. }
  112. /**
  113. * Reads data from the object referenced by handle.
  114. *
  115. * On error, the contents of *data are undefined.
  116. */
  117. int
  118. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  119. struct drm_file *file_priv)
  120. {
  121. struct drm_i915_gem_pread *args = data;
  122. struct drm_gem_object *obj;
  123. struct drm_i915_gem_object *obj_priv;
  124. ssize_t read;
  125. loff_t offset;
  126. int ret;
  127. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  128. if (obj == NULL)
  129. return -EBADF;
  130. obj_priv = obj->driver_private;
  131. /* Bounds check source.
  132. *
  133. * XXX: This could use review for overflow issues...
  134. */
  135. if (args->offset > obj->size || args->size > obj->size ||
  136. args->offset + args->size > obj->size) {
  137. drm_gem_object_unreference(obj);
  138. return -EINVAL;
  139. }
  140. mutex_lock(&dev->struct_mutex);
  141. ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
  142. I915_GEM_DOMAIN_CPU, 0);
  143. if (ret != 0) {
  144. drm_gem_object_unreference(obj);
  145. mutex_unlock(&dev->struct_mutex);
  146. return ret;
  147. }
  148. offset = args->offset;
  149. read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
  150. args->size, &offset);
  151. if (read != args->size) {
  152. drm_gem_object_unreference(obj);
  153. mutex_unlock(&dev->struct_mutex);
  154. if (read < 0)
  155. return read;
  156. else
  157. return -EINVAL;
  158. }
  159. drm_gem_object_unreference(obj);
  160. mutex_unlock(&dev->struct_mutex);
  161. return 0;
  162. }
  163. /* This is the fast write path which cannot handle
  164. * page faults in the source data
  165. */
  166. static inline int
  167. fast_user_write(struct io_mapping *mapping,
  168. loff_t page_base, int page_offset,
  169. char __user *user_data,
  170. int length)
  171. {
  172. char *vaddr_atomic;
  173. unsigned long unwritten;
  174. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  175. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  176. user_data, length);
  177. io_mapping_unmap_atomic(vaddr_atomic);
  178. if (unwritten)
  179. return -EFAULT;
  180. return 0;
  181. }
  182. /* Here's the write path which can sleep for
  183. * page faults
  184. */
  185. static inline int
  186. slow_user_write(struct io_mapping *mapping,
  187. loff_t page_base, int page_offset,
  188. char __user *user_data,
  189. int length)
  190. {
  191. char __iomem *vaddr;
  192. unsigned long unwritten;
  193. vaddr = io_mapping_map_wc(mapping, page_base);
  194. if (vaddr == NULL)
  195. return -EFAULT;
  196. unwritten = __copy_from_user(vaddr + page_offset,
  197. user_data, length);
  198. io_mapping_unmap(vaddr);
  199. if (unwritten)
  200. return -EFAULT;
  201. return 0;
  202. }
  203. static int
  204. i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  205. struct drm_i915_gem_pwrite *args,
  206. struct drm_file *file_priv)
  207. {
  208. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  209. drm_i915_private_t *dev_priv = dev->dev_private;
  210. ssize_t remain;
  211. loff_t offset, page_base;
  212. char __user *user_data;
  213. int page_offset, page_length;
  214. int ret;
  215. user_data = (char __user *) (uintptr_t) args->data_ptr;
  216. remain = args->size;
  217. if (!access_ok(VERIFY_READ, user_data, remain))
  218. return -EFAULT;
  219. mutex_lock(&dev->struct_mutex);
  220. ret = i915_gem_object_pin(obj, 0);
  221. if (ret) {
  222. mutex_unlock(&dev->struct_mutex);
  223. return ret;
  224. }
  225. ret = i915_gem_set_domain(obj, file_priv,
  226. I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
  227. if (ret)
  228. goto fail;
  229. obj_priv = obj->driver_private;
  230. offset = obj_priv->gtt_offset + args->offset;
  231. obj_priv->dirty = 1;
  232. while (remain > 0) {
  233. /* Operation in this page
  234. *
  235. * page_base = page offset within aperture
  236. * page_offset = offset within page
  237. * page_length = bytes to copy for this page
  238. */
  239. page_base = (offset & ~(PAGE_SIZE-1));
  240. page_offset = offset & (PAGE_SIZE-1);
  241. page_length = remain;
  242. if ((page_offset + remain) > PAGE_SIZE)
  243. page_length = PAGE_SIZE - page_offset;
  244. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  245. page_offset, user_data, page_length);
  246. /* If we get a fault while copying data, then (presumably) our
  247. * source page isn't available. In this case, use the
  248. * non-atomic function
  249. */
  250. if (ret) {
  251. ret = slow_user_write (dev_priv->mm.gtt_mapping,
  252. page_base, page_offset,
  253. user_data, page_length);
  254. if (ret)
  255. goto fail;
  256. }
  257. remain -= page_length;
  258. user_data += page_length;
  259. offset += page_length;
  260. }
  261. fail:
  262. i915_gem_object_unpin(obj);
  263. mutex_unlock(&dev->struct_mutex);
  264. return ret;
  265. }
  266. static int
  267. i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  268. struct drm_i915_gem_pwrite *args,
  269. struct drm_file *file_priv)
  270. {
  271. int ret;
  272. loff_t offset;
  273. ssize_t written;
  274. mutex_lock(&dev->struct_mutex);
  275. ret = i915_gem_set_domain(obj, file_priv,
  276. I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
  277. if (ret) {
  278. mutex_unlock(&dev->struct_mutex);
  279. return ret;
  280. }
  281. offset = args->offset;
  282. written = vfs_write(obj->filp,
  283. (char __user *)(uintptr_t) args->data_ptr,
  284. args->size, &offset);
  285. if (written != args->size) {
  286. mutex_unlock(&dev->struct_mutex);
  287. if (written < 0)
  288. return written;
  289. else
  290. return -EINVAL;
  291. }
  292. mutex_unlock(&dev->struct_mutex);
  293. return 0;
  294. }
  295. /**
  296. * Writes data to the object referenced by handle.
  297. *
  298. * On error, the contents of the buffer that were to be modified are undefined.
  299. */
  300. int
  301. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  302. struct drm_file *file_priv)
  303. {
  304. struct drm_i915_gem_pwrite *args = data;
  305. struct drm_gem_object *obj;
  306. struct drm_i915_gem_object *obj_priv;
  307. int ret = 0;
  308. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  309. if (obj == NULL)
  310. return -EBADF;
  311. obj_priv = obj->driver_private;
  312. /* Bounds check destination.
  313. *
  314. * XXX: This could use review for overflow issues...
  315. */
  316. if (args->offset > obj->size || args->size > obj->size ||
  317. args->offset + args->size > obj->size) {
  318. drm_gem_object_unreference(obj);
  319. return -EINVAL;
  320. }
  321. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  322. * it would end up going through the fenced access, and we'll get
  323. * different detiling behavior between reading and writing.
  324. * pread/pwrite currently are reading and writing from the CPU
  325. * perspective, requiring manual detiling by the client.
  326. */
  327. if (obj_priv->tiling_mode == I915_TILING_NONE &&
  328. dev->gtt_total != 0)
  329. ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
  330. else
  331. ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
  332. #if WATCH_PWRITE
  333. if (ret)
  334. DRM_INFO("pwrite failed %d\n", ret);
  335. #endif
  336. drm_gem_object_unreference(obj);
  337. return ret;
  338. }
  339. /**
  340. * Called when user space prepares to use an object
  341. */
  342. int
  343. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  344. struct drm_file *file_priv)
  345. {
  346. struct drm_i915_gem_set_domain *args = data;
  347. struct drm_gem_object *obj;
  348. int ret;
  349. if (!(dev->driver->driver_features & DRIVER_GEM))
  350. return -ENODEV;
  351. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  352. if (obj == NULL)
  353. return -EBADF;
  354. mutex_lock(&dev->struct_mutex);
  355. #if WATCH_BUF
  356. DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
  357. obj, obj->size, args->read_domains, args->write_domain);
  358. #endif
  359. ret = i915_gem_set_domain(obj, file_priv,
  360. args->read_domains, args->write_domain);
  361. drm_gem_object_unreference(obj);
  362. mutex_unlock(&dev->struct_mutex);
  363. return ret;
  364. }
  365. /**
  366. * Called when user space has done writes to this buffer
  367. */
  368. int
  369. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  370. struct drm_file *file_priv)
  371. {
  372. struct drm_i915_gem_sw_finish *args = data;
  373. struct drm_gem_object *obj;
  374. struct drm_i915_gem_object *obj_priv;
  375. int ret = 0;
  376. if (!(dev->driver->driver_features & DRIVER_GEM))
  377. return -ENODEV;
  378. mutex_lock(&dev->struct_mutex);
  379. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  380. if (obj == NULL) {
  381. mutex_unlock(&dev->struct_mutex);
  382. return -EBADF;
  383. }
  384. #if WATCH_BUF
  385. DRM_INFO("%s: sw_finish %d (%p %d)\n",
  386. __func__, args->handle, obj, obj->size);
  387. #endif
  388. obj_priv = obj->driver_private;
  389. /* Pinned buffers may be scanout, so flush the cache */
  390. if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
  391. i915_gem_clflush_object(obj);
  392. drm_agp_chipset_flush(dev);
  393. }
  394. drm_gem_object_unreference(obj);
  395. mutex_unlock(&dev->struct_mutex);
  396. return ret;
  397. }
  398. /**
  399. * Maps the contents of an object, returning the address it is mapped
  400. * into.
  401. *
  402. * While the mapping holds a reference on the contents of the object, it doesn't
  403. * imply a ref on the object itself.
  404. */
  405. int
  406. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  407. struct drm_file *file_priv)
  408. {
  409. struct drm_i915_gem_mmap *args = data;
  410. struct drm_gem_object *obj;
  411. loff_t offset;
  412. unsigned long addr;
  413. if (!(dev->driver->driver_features & DRIVER_GEM))
  414. return -ENODEV;
  415. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  416. if (obj == NULL)
  417. return -EBADF;
  418. offset = args->offset;
  419. down_write(&current->mm->mmap_sem);
  420. addr = do_mmap(obj->filp, 0, args->size,
  421. PROT_READ | PROT_WRITE, MAP_SHARED,
  422. args->offset);
  423. up_write(&current->mm->mmap_sem);
  424. mutex_lock(&dev->struct_mutex);
  425. drm_gem_object_unreference(obj);
  426. mutex_unlock(&dev->struct_mutex);
  427. if (IS_ERR((void *)addr))
  428. return addr;
  429. args->addr_ptr = (uint64_t) addr;
  430. return 0;
  431. }
  432. static void
  433. i915_gem_object_free_page_list(struct drm_gem_object *obj)
  434. {
  435. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  436. int page_count = obj->size / PAGE_SIZE;
  437. int i;
  438. if (obj_priv->page_list == NULL)
  439. return;
  440. for (i = 0; i < page_count; i++)
  441. if (obj_priv->page_list[i] != NULL) {
  442. if (obj_priv->dirty)
  443. set_page_dirty(obj_priv->page_list[i]);
  444. mark_page_accessed(obj_priv->page_list[i]);
  445. page_cache_release(obj_priv->page_list[i]);
  446. }
  447. obj_priv->dirty = 0;
  448. drm_free(obj_priv->page_list,
  449. page_count * sizeof(struct page *),
  450. DRM_MEM_DRIVER);
  451. obj_priv->page_list = NULL;
  452. }
  453. static void
  454. i915_gem_object_move_to_active(struct drm_gem_object *obj)
  455. {
  456. struct drm_device *dev = obj->dev;
  457. drm_i915_private_t *dev_priv = dev->dev_private;
  458. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  459. /* Add a reference if we're newly entering the active list. */
  460. if (!obj_priv->active) {
  461. drm_gem_object_reference(obj);
  462. obj_priv->active = 1;
  463. }
  464. /* Move from whatever list we were on to the tail of execution. */
  465. list_move_tail(&obj_priv->list,
  466. &dev_priv->mm.active_list);
  467. }
  468. static void
  469. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  470. {
  471. struct drm_device *dev = obj->dev;
  472. drm_i915_private_t *dev_priv = dev->dev_private;
  473. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  474. i915_verify_inactive(dev, __FILE__, __LINE__);
  475. if (obj_priv->pin_count != 0)
  476. list_del_init(&obj_priv->list);
  477. else
  478. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  479. if (obj_priv->active) {
  480. obj_priv->active = 0;
  481. drm_gem_object_unreference(obj);
  482. }
  483. i915_verify_inactive(dev, __FILE__, __LINE__);
  484. }
  485. /**
  486. * Creates a new sequence number, emitting a write of it to the status page
  487. * plus an interrupt, which will trigger i915_user_interrupt_handler.
  488. *
  489. * Must be called with struct_lock held.
  490. *
  491. * Returned sequence numbers are nonzero on success.
  492. */
  493. static uint32_t
  494. i915_add_request(struct drm_device *dev, uint32_t flush_domains)
  495. {
  496. drm_i915_private_t *dev_priv = dev->dev_private;
  497. struct drm_i915_gem_request *request;
  498. uint32_t seqno;
  499. int was_empty;
  500. RING_LOCALS;
  501. request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
  502. if (request == NULL)
  503. return 0;
  504. /* Grab the seqno we're going to make this request be, and bump the
  505. * next (skipping 0 so it can be the reserved no-seqno value).
  506. */
  507. seqno = dev_priv->mm.next_gem_seqno;
  508. dev_priv->mm.next_gem_seqno++;
  509. if (dev_priv->mm.next_gem_seqno == 0)
  510. dev_priv->mm.next_gem_seqno++;
  511. BEGIN_LP_RING(4);
  512. OUT_RING(MI_STORE_DWORD_INDEX);
  513. OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  514. OUT_RING(seqno);
  515. OUT_RING(MI_USER_INTERRUPT);
  516. ADVANCE_LP_RING();
  517. DRM_DEBUG("%d\n", seqno);
  518. request->seqno = seqno;
  519. request->emitted_jiffies = jiffies;
  520. request->flush_domains = flush_domains;
  521. was_empty = list_empty(&dev_priv->mm.request_list);
  522. list_add_tail(&request->list, &dev_priv->mm.request_list);
  523. if (was_empty && !dev_priv->mm.suspended)
  524. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  525. return seqno;
  526. }
  527. /**
  528. * Command execution barrier
  529. *
  530. * Ensures that all commands in the ring are finished
  531. * before signalling the CPU
  532. */
  533. static uint32_t
  534. i915_retire_commands(struct drm_device *dev)
  535. {
  536. drm_i915_private_t *dev_priv = dev->dev_private;
  537. uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  538. uint32_t flush_domains = 0;
  539. RING_LOCALS;
  540. /* The sampler always gets flushed on i965 (sigh) */
  541. if (IS_I965G(dev))
  542. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  543. BEGIN_LP_RING(2);
  544. OUT_RING(cmd);
  545. OUT_RING(0); /* noop */
  546. ADVANCE_LP_RING();
  547. return flush_domains;
  548. }
  549. /**
  550. * Moves buffers associated only with the given active seqno from the active
  551. * to inactive list, potentially freeing them.
  552. */
  553. static void
  554. i915_gem_retire_request(struct drm_device *dev,
  555. struct drm_i915_gem_request *request)
  556. {
  557. drm_i915_private_t *dev_priv = dev->dev_private;
  558. /* Move any buffers on the active list that are no longer referenced
  559. * by the ringbuffer to the flushing/inactive lists as appropriate.
  560. */
  561. while (!list_empty(&dev_priv->mm.active_list)) {
  562. struct drm_gem_object *obj;
  563. struct drm_i915_gem_object *obj_priv;
  564. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  565. struct drm_i915_gem_object,
  566. list);
  567. obj = obj_priv->obj;
  568. /* If the seqno being retired doesn't match the oldest in the
  569. * list, then the oldest in the list must still be newer than
  570. * this seqno.
  571. */
  572. if (obj_priv->last_rendering_seqno != request->seqno)
  573. return;
  574. #if WATCH_LRU
  575. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  576. __func__, request->seqno, obj);
  577. #endif
  578. if (obj->write_domain != 0) {
  579. list_move_tail(&obj_priv->list,
  580. &dev_priv->mm.flushing_list);
  581. } else {
  582. i915_gem_object_move_to_inactive(obj);
  583. }
  584. }
  585. if (request->flush_domains != 0) {
  586. struct drm_i915_gem_object *obj_priv, *next;
  587. /* Clear the write domain and activity from any buffers
  588. * that are just waiting for a flush matching the one retired.
  589. */
  590. list_for_each_entry_safe(obj_priv, next,
  591. &dev_priv->mm.flushing_list, list) {
  592. struct drm_gem_object *obj = obj_priv->obj;
  593. if (obj->write_domain & request->flush_domains) {
  594. obj->write_domain = 0;
  595. i915_gem_object_move_to_inactive(obj);
  596. }
  597. }
  598. }
  599. }
  600. /**
  601. * Returns true if seq1 is later than seq2.
  602. */
  603. static int
  604. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  605. {
  606. return (int32_t)(seq1 - seq2) >= 0;
  607. }
  608. uint32_t
  609. i915_get_gem_seqno(struct drm_device *dev)
  610. {
  611. drm_i915_private_t *dev_priv = dev->dev_private;
  612. return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
  613. }
  614. /**
  615. * This function clears the request list as sequence numbers are passed.
  616. */
  617. void
  618. i915_gem_retire_requests(struct drm_device *dev)
  619. {
  620. drm_i915_private_t *dev_priv = dev->dev_private;
  621. uint32_t seqno;
  622. seqno = i915_get_gem_seqno(dev);
  623. while (!list_empty(&dev_priv->mm.request_list)) {
  624. struct drm_i915_gem_request *request;
  625. uint32_t retiring_seqno;
  626. request = list_first_entry(&dev_priv->mm.request_list,
  627. struct drm_i915_gem_request,
  628. list);
  629. retiring_seqno = request->seqno;
  630. if (i915_seqno_passed(seqno, retiring_seqno) ||
  631. dev_priv->mm.wedged) {
  632. i915_gem_retire_request(dev, request);
  633. list_del(&request->list);
  634. drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
  635. } else
  636. break;
  637. }
  638. }
  639. void
  640. i915_gem_retire_work_handler(struct work_struct *work)
  641. {
  642. drm_i915_private_t *dev_priv;
  643. struct drm_device *dev;
  644. dev_priv = container_of(work, drm_i915_private_t,
  645. mm.retire_work.work);
  646. dev = dev_priv->dev;
  647. mutex_lock(&dev->struct_mutex);
  648. i915_gem_retire_requests(dev);
  649. if (!dev_priv->mm.suspended &&
  650. !list_empty(&dev_priv->mm.request_list))
  651. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  652. mutex_unlock(&dev->struct_mutex);
  653. }
  654. /**
  655. * Waits for a sequence number to be signaled, and cleans up the
  656. * request and object lists appropriately for that event.
  657. */
  658. static int
  659. i915_wait_request(struct drm_device *dev, uint32_t seqno)
  660. {
  661. drm_i915_private_t *dev_priv = dev->dev_private;
  662. int ret = 0;
  663. BUG_ON(seqno == 0);
  664. if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
  665. dev_priv->mm.waiting_gem_seqno = seqno;
  666. i915_user_irq_get(dev);
  667. ret = wait_event_interruptible(dev_priv->irq_queue,
  668. i915_seqno_passed(i915_get_gem_seqno(dev),
  669. seqno) ||
  670. dev_priv->mm.wedged);
  671. i915_user_irq_put(dev);
  672. dev_priv->mm.waiting_gem_seqno = 0;
  673. }
  674. if (dev_priv->mm.wedged)
  675. ret = -EIO;
  676. if (ret && ret != -ERESTARTSYS)
  677. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  678. __func__, ret, seqno, i915_get_gem_seqno(dev));
  679. /* Directly dispatch request retiring. While we have the work queue
  680. * to handle this, the waiter on a request often wants an associated
  681. * buffer to have made it to the inactive list, and we would need
  682. * a separate wait queue to handle that.
  683. */
  684. if (ret == 0)
  685. i915_gem_retire_requests(dev);
  686. return ret;
  687. }
  688. static void
  689. i915_gem_flush(struct drm_device *dev,
  690. uint32_t invalidate_domains,
  691. uint32_t flush_domains)
  692. {
  693. drm_i915_private_t *dev_priv = dev->dev_private;
  694. uint32_t cmd;
  695. RING_LOCALS;
  696. #if WATCH_EXEC
  697. DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
  698. invalidate_domains, flush_domains);
  699. #endif
  700. if (flush_domains & I915_GEM_DOMAIN_CPU)
  701. drm_agp_chipset_flush(dev);
  702. if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
  703. I915_GEM_DOMAIN_GTT)) {
  704. /*
  705. * read/write caches:
  706. *
  707. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  708. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  709. * also flushed at 2d versus 3d pipeline switches.
  710. *
  711. * read-only caches:
  712. *
  713. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  714. * MI_READ_FLUSH is set, and is always flushed on 965.
  715. *
  716. * I915_GEM_DOMAIN_COMMAND may not exist?
  717. *
  718. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  719. * invalidated when MI_EXE_FLUSH is set.
  720. *
  721. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  722. * invalidated with every MI_FLUSH.
  723. *
  724. * TLBs:
  725. *
  726. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  727. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  728. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  729. * are flushed at any MI_FLUSH.
  730. */
  731. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  732. if ((invalidate_domains|flush_domains) &
  733. I915_GEM_DOMAIN_RENDER)
  734. cmd &= ~MI_NO_WRITE_FLUSH;
  735. if (!IS_I965G(dev)) {
  736. /*
  737. * On the 965, the sampler cache always gets flushed
  738. * and this bit is reserved.
  739. */
  740. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  741. cmd |= MI_READ_FLUSH;
  742. }
  743. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  744. cmd |= MI_EXE_FLUSH;
  745. #if WATCH_EXEC
  746. DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
  747. #endif
  748. BEGIN_LP_RING(2);
  749. OUT_RING(cmd);
  750. OUT_RING(0); /* noop */
  751. ADVANCE_LP_RING();
  752. }
  753. }
  754. /**
  755. * Ensures that all rendering to the object has completed and the object is
  756. * safe to unbind from the GTT or access from the CPU.
  757. */
  758. static int
  759. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  760. {
  761. struct drm_device *dev = obj->dev;
  762. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  763. int ret;
  764. /* If there are writes queued to the buffer, flush and
  765. * create a new seqno to wait for.
  766. */
  767. if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
  768. uint32_t write_domain = obj->write_domain;
  769. #if WATCH_BUF
  770. DRM_INFO("%s: flushing object %p from write domain %08x\n",
  771. __func__, obj, write_domain);
  772. #endif
  773. i915_gem_flush(dev, 0, write_domain);
  774. i915_gem_object_move_to_active(obj);
  775. obj_priv->last_rendering_seqno = i915_add_request(dev,
  776. write_domain);
  777. BUG_ON(obj_priv->last_rendering_seqno == 0);
  778. #if WATCH_LRU
  779. DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
  780. #endif
  781. }
  782. /* If there is rendering queued on the buffer being evicted, wait for
  783. * it.
  784. */
  785. if (obj_priv->active) {
  786. #if WATCH_BUF
  787. DRM_INFO("%s: object %p wait for seqno %08x\n",
  788. __func__, obj, obj_priv->last_rendering_seqno);
  789. #endif
  790. ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
  791. if (ret != 0)
  792. return ret;
  793. }
  794. return 0;
  795. }
  796. /**
  797. * Unbinds an object from the GTT aperture.
  798. */
  799. static int
  800. i915_gem_object_unbind(struct drm_gem_object *obj)
  801. {
  802. struct drm_device *dev = obj->dev;
  803. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  804. int ret = 0;
  805. #if WATCH_BUF
  806. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  807. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  808. #endif
  809. if (obj_priv->gtt_space == NULL)
  810. return 0;
  811. if (obj_priv->pin_count != 0) {
  812. DRM_ERROR("Attempting to unbind pinned buffer\n");
  813. return -EINVAL;
  814. }
  815. /* Wait for any rendering to complete
  816. */
  817. ret = i915_gem_object_wait_rendering(obj);
  818. if (ret) {
  819. DRM_ERROR("wait_rendering failed: %d\n", ret);
  820. return ret;
  821. }
  822. /* Move the object to the CPU domain to ensure that
  823. * any possible CPU writes while it's not in the GTT
  824. * are flushed when we go to remap it. This will
  825. * also ensure that all pending GPU writes are finished
  826. * before we unbind.
  827. */
  828. ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
  829. I915_GEM_DOMAIN_CPU);
  830. if (ret) {
  831. DRM_ERROR("set_domain failed: %d\n", ret);
  832. return ret;
  833. }
  834. if (obj_priv->agp_mem != NULL) {
  835. drm_unbind_agp(obj_priv->agp_mem);
  836. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  837. obj_priv->agp_mem = NULL;
  838. }
  839. BUG_ON(obj_priv->active);
  840. i915_gem_object_free_page_list(obj);
  841. if (obj_priv->gtt_space) {
  842. atomic_dec(&dev->gtt_count);
  843. atomic_sub(obj->size, &dev->gtt_memory);
  844. drm_mm_put_block(obj_priv->gtt_space);
  845. obj_priv->gtt_space = NULL;
  846. }
  847. /* Remove ourselves from the LRU list if present. */
  848. if (!list_empty(&obj_priv->list))
  849. list_del_init(&obj_priv->list);
  850. return 0;
  851. }
  852. static int
  853. i915_gem_evict_something(struct drm_device *dev)
  854. {
  855. drm_i915_private_t *dev_priv = dev->dev_private;
  856. struct drm_gem_object *obj;
  857. struct drm_i915_gem_object *obj_priv;
  858. int ret = 0;
  859. for (;;) {
  860. /* If there's an inactive buffer available now, grab it
  861. * and be done.
  862. */
  863. if (!list_empty(&dev_priv->mm.inactive_list)) {
  864. obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
  865. struct drm_i915_gem_object,
  866. list);
  867. obj = obj_priv->obj;
  868. BUG_ON(obj_priv->pin_count != 0);
  869. #if WATCH_LRU
  870. DRM_INFO("%s: evicting %p\n", __func__, obj);
  871. #endif
  872. BUG_ON(obj_priv->active);
  873. /* Wait on the rendering and unbind the buffer. */
  874. ret = i915_gem_object_unbind(obj);
  875. break;
  876. }
  877. /* If we didn't get anything, but the ring is still processing
  878. * things, wait for one of those things to finish and hopefully
  879. * leave us a buffer to evict.
  880. */
  881. if (!list_empty(&dev_priv->mm.request_list)) {
  882. struct drm_i915_gem_request *request;
  883. request = list_first_entry(&dev_priv->mm.request_list,
  884. struct drm_i915_gem_request,
  885. list);
  886. ret = i915_wait_request(dev, request->seqno);
  887. if (ret)
  888. break;
  889. /* if waiting caused an object to become inactive,
  890. * then loop around and wait for it. Otherwise, we
  891. * assume that waiting freed and unbound something,
  892. * so there should now be some space in the GTT
  893. */
  894. if (!list_empty(&dev_priv->mm.inactive_list))
  895. continue;
  896. break;
  897. }
  898. /* If we didn't have anything on the request list but there
  899. * are buffers awaiting a flush, emit one and try again.
  900. * When we wait on it, those buffers waiting for that flush
  901. * will get moved to inactive.
  902. */
  903. if (!list_empty(&dev_priv->mm.flushing_list)) {
  904. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  905. struct drm_i915_gem_object,
  906. list);
  907. obj = obj_priv->obj;
  908. i915_gem_flush(dev,
  909. obj->write_domain,
  910. obj->write_domain);
  911. i915_add_request(dev, obj->write_domain);
  912. obj = NULL;
  913. continue;
  914. }
  915. DRM_ERROR("inactive empty %d request empty %d "
  916. "flushing empty %d\n",
  917. list_empty(&dev_priv->mm.inactive_list),
  918. list_empty(&dev_priv->mm.request_list),
  919. list_empty(&dev_priv->mm.flushing_list));
  920. /* If we didn't do any of the above, there's nothing to be done
  921. * and we just can't fit it in.
  922. */
  923. return -ENOMEM;
  924. }
  925. return ret;
  926. }
  927. static int
  928. i915_gem_object_get_page_list(struct drm_gem_object *obj)
  929. {
  930. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  931. int page_count, i;
  932. struct address_space *mapping;
  933. struct inode *inode;
  934. struct page *page;
  935. int ret;
  936. if (obj_priv->page_list)
  937. return 0;
  938. /* Get the list of pages out of our struct file. They'll be pinned
  939. * at this point until we release them.
  940. */
  941. page_count = obj->size / PAGE_SIZE;
  942. BUG_ON(obj_priv->page_list != NULL);
  943. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  944. DRM_MEM_DRIVER);
  945. if (obj_priv->page_list == NULL) {
  946. DRM_ERROR("Faled to allocate page list\n");
  947. return -ENOMEM;
  948. }
  949. inode = obj->filp->f_path.dentry->d_inode;
  950. mapping = inode->i_mapping;
  951. for (i = 0; i < page_count; i++) {
  952. page = read_mapping_page(mapping, i, NULL);
  953. if (IS_ERR(page)) {
  954. ret = PTR_ERR(page);
  955. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  956. i915_gem_object_free_page_list(obj);
  957. return ret;
  958. }
  959. obj_priv->page_list[i] = page;
  960. }
  961. return 0;
  962. }
  963. /**
  964. * Finds free space in the GTT aperture and binds the object there.
  965. */
  966. static int
  967. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  968. {
  969. struct drm_device *dev = obj->dev;
  970. drm_i915_private_t *dev_priv = dev->dev_private;
  971. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  972. struct drm_mm_node *free_space;
  973. int page_count, ret;
  974. if (alignment == 0)
  975. alignment = PAGE_SIZE;
  976. if (alignment & (PAGE_SIZE - 1)) {
  977. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  978. return -EINVAL;
  979. }
  980. search_free:
  981. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  982. obj->size, alignment, 0);
  983. if (free_space != NULL) {
  984. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  985. alignment);
  986. if (obj_priv->gtt_space != NULL) {
  987. obj_priv->gtt_space->private = obj;
  988. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  989. }
  990. }
  991. if (obj_priv->gtt_space == NULL) {
  992. /* If the gtt is empty and we're still having trouble
  993. * fitting our object in, we're out of memory.
  994. */
  995. #if WATCH_LRU
  996. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  997. #endif
  998. if (list_empty(&dev_priv->mm.inactive_list) &&
  999. list_empty(&dev_priv->mm.flushing_list) &&
  1000. list_empty(&dev_priv->mm.active_list)) {
  1001. DRM_ERROR("GTT full, but LRU list empty\n");
  1002. return -ENOMEM;
  1003. }
  1004. ret = i915_gem_evict_something(dev);
  1005. if (ret != 0) {
  1006. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1007. return ret;
  1008. }
  1009. goto search_free;
  1010. }
  1011. #if WATCH_BUF
  1012. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1013. obj->size, obj_priv->gtt_offset);
  1014. #endif
  1015. ret = i915_gem_object_get_page_list(obj);
  1016. if (ret) {
  1017. drm_mm_put_block(obj_priv->gtt_space);
  1018. obj_priv->gtt_space = NULL;
  1019. return ret;
  1020. }
  1021. page_count = obj->size / PAGE_SIZE;
  1022. /* Create an AGP memory structure pointing at our pages, and bind it
  1023. * into the GTT.
  1024. */
  1025. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1026. obj_priv->page_list,
  1027. page_count,
  1028. obj_priv->gtt_offset,
  1029. obj_priv->agp_type);
  1030. if (obj_priv->agp_mem == NULL) {
  1031. i915_gem_object_free_page_list(obj);
  1032. drm_mm_put_block(obj_priv->gtt_space);
  1033. obj_priv->gtt_space = NULL;
  1034. return -ENOMEM;
  1035. }
  1036. atomic_inc(&dev->gtt_count);
  1037. atomic_add(obj->size, &dev->gtt_memory);
  1038. /* Assert that the object is not currently in any GPU domain. As it
  1039. * wasn't in the GTT, there shouldn't be any way it could have been in
  1040. * a GPU cache
  1041. */
  1042. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1043. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1044. return 0;
  1045. }
  1046. void
  1047. i915_gem_clflush_object(struct drm_gem_object *obj)
  1048. {
  1049. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1050. /* If we don't have a page list set up, then we're not pinned
  1051. * to GPU, and we can ignore the cache flush because it'll happen
  1052. * again at bind time.
  1053. */
  1054. if (obj_priv->page_list == NULL)
  1055. return;
  1056. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1057. }
  1058. /*
  1059. * Set the next domain for the specified object. This
  1060. * may not actually perform the necessary flushing/invaliding though,
  1061. * as that may want to be batched with other set_domain operations
  1062. *
  1063. * This is (we hope) the only really tricky part of gem. The goal
  1064. * is fairly simple -- track which caches hold bits of the object
  1065. * and make sure they remain coherent. A few concrete examples may
  1066. * help to explain how it works. For shorthand, we use the notation
  1067. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1068. * a pair of read and write domain masks.
  1069. *
  1070. * Case 1: the batch buffer
  1071. *
  1072. * 1. Allocated
  1073. * 2. Written by CPU
  1074. * 3. Mapped to GTT
  1075. * 4. Read by GPU
  1076. * 5. Unmapped from GTT
  1077. * 6. Freed
  1078. *
  1079. * Let's take these a step at a time
  1080. *
  1081. * 1. Allocated
  1082. * Pages allocated from the kernel may still have
  1083. * cache contents, so we set them to (CPU, CPU) always.
  1084. * 2. Written by CPU (using pwrite)
  1085. * The pwrite function calls set_domain (CPU, CPU) and
  1086. * this function does nothing (as nothing changes)
  1087. * 3. Mapped by GTT
  1088. * This function asserts that the object is not
  1089. * currently in any GPU-based read or write domains
  1090. * 4. Read by GPU
  1091. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1092. * As write_domain is zero, this function adds in the
  1093. * current read domains (CPU+COMMAND, 0).
  1094. * flush_domains is set to CPU.
  1095. * invalidate_domains is set to COMMAND
  1096. * clflush is run to get data out of the CPU caches
  1097. * then i915_dev_set_domain calls i915_gem_flush to
  1098. * emit an MI_FLUSH and drm_agp_chipset_flush
  1099. * 5. Unmapped from GTT
  1100. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1101. * flush_domains and invalidate_domains end up both zero
  1102. * so no flushing/invalidating happens
  1103. * 6. Freed
  1104. * yay, done
  1105. *
  1106. * Case 2: The shared render buffer
  1107. *
  1108. * 1. Allocated
  1109. * 2. Mapped to GTT
  1110. * 3. Read/written by GPU
  1111. * 4. set_domain to (CPU,CPU)
  1112. * 5. Read/written by CPU
  1113. * 6. Read/written by GPU
  1114. *
  1115. * 1. Allocated
  1116. * Same as last example, (CPU, CPU)
  1117. * 2. Mapped to GTT
  1118. * Nothing changes (assertions find that it is not in the GPU)
  1119. * 3. Read/written by GPU
  1120. * execbuffer calls set_domain (RENDER, RENDER)
  1121. * flush_domains gets CPU
  1122. * invalidate_domains gets GPU
  1123. * clflush (obj)
  1124. * MI_FLUSH and drm_agp_chipset_flush
  1125. * 4. set_domain (CPU, CPU)
  1126. * flush_domains gets GPU
  1127. * invalidate_domains gets CPU
  1128. * wait_rendering (obj) to make sure all drawing is complete.
  1129. * This will include an MI_FLUSH to get the data from GPU
  1130. * to memory
  1131. * clflush (obj) to invalidate the CPU cache
  1132. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1133. * 5. Read/written by CPU
  1134. * cache lines are loaded and dirtied
  1135. * 6. Read written by GPU
  1136. * Same as last GPU access
  1137. *
  1138. * Case 3: The constant buffer
  1139. *
  1140. * 1. Allocated
  1141. * 2. Written by CPU
  1142. * 3. Read by GPU
  1143. * 4. Updated (written) by CPU again
  1144. * 5. Read by GPU
  1145. *
  1146. * 1. Allocated
  1147. * (CPU, CPU)
  1148. * 2. Written by CPU
  1149. * (CPU, CPU)
  1150. * 3. Read by GPU
  1151. * (CPU+RENDER, 0)
  1152. * flush_domains = CPU
  1153. * invalidate_domains = RENDER
  1154. * clflush (obj)
  1155. * MI_FLUSH
  1156. * drm_agp_chipset_flush
  1157. * 4. Updated (written) by CPU again
  1158. * (CPU, CPU)
  1159. * flush_domains = 0 (no previous write domain)
  1160. * invalidate_domains = 0 (no new read domains)
  1161. * 5. Read by GPU
  1162. * (CPU+RENDER, 0)
  1163. * flush_domains = CPU
  1164. * invalidate_domains = RENDER
  1165. * clflush (obj)
  1166. * MI_FLUSH
  1167. * drm_agp_chipset_flush
  1168. */
  1169. static int
  1170. i915_gem_object_set_domain(struct drm_gem_object *obj,
  1171. uint32_t read_domains,
  1172. uint32_t write_domain)
  1173. {
  1174. struct drm_device *dev = obj->dev;
  1175. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1176. uint32_t invalidate_domains = 0;
  1177. uint32_t flush_domains = 0;
  1178. int ret;
  1179. #if WATCH_BUF
  1180. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1181. __func__, obj,
  1182. obj->read_domains, read_domains,
  1183. obj->write_domain, write_domain);
  1184. #endif
  1185. /*
  1186. * If the object isn't moving to a new write domain,
  1187. * let the object stay in multiple read domains
  1188. */
  1189. if (write_domain == 0)
  1190. read_domains |= obj->read_domains;
  1191. else
  1192. obj_priv->dirty = 1;
  1193. /*
  1194. * Flush the current write domain if
  1195. * the new read domains don't match. Invalidate
  1196. * any read domains which differ from the old
  1197. * write domain
  1198. */
  1199. if (obj->write_domain && obj->write_domain != read_domains) {
  1200. flush_domains |= obj->write_domain;
  1201. invalidate_domains |= read_domains & ~obj->write_domain;
  1202. }
  1203. /*
  1204. * Invalidate any read caches which may have
  1205. * stale data. That is, any new read domains.
  1206. */
  1207. invalidate_domains |= read_domains & ~obj->read_domains;
  1208. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1209. #if WATCH_BUF
  1210. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1211. __func__, flush_domains, invalidate_domains);
  1212. #endif
  1213. /*
  1214. * If we're invaliding the CPU cache and flushing a GPU cache,
  1215. * then pause for rendering so that the GPU caches will be
  1216. * flushed before the cpu cache is invalidated
  1217. */
  1218. if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
  1219. (flush_domains & ~(I915_GEM_DOMAIN_CPU |
  1220. I915_GEM_DOMAIN_GTT))) {
  1221. ret = i915_gem_object_wait_rendering(obj);
  1222. if (ret)
  1223. return ret;
  1224. }
  1225. i915_gem_clflush_object(obj);
  1226. }
  1227. if ((write_domain | flush_domains) != 0)
  1228. obj->write_domain = write_domain;
  1229. /* If we're invalidating the CPU domain, clear the per-page CPU
  1230. * domain list as well.
  1231. */
  1232. if (obj_priv->page_cpu_valid != NULL &&
  1233. (write_domain != 0 ||
  1234. read_domains & I915_GEM_DOMAIN_CPU)) {
  1235. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1236. DRM_MEM_DRIVER);
  1237. obj_priv->page_cpu_valid = NULL;
  1238. }
  1239. obj->read_domains = read_domains;
  1240. dev->invalidate_domains |= invalidate_domains;
  1241. dev->flush_domains |= flush_domains;
  1242. #if WATCH_BUF
  1243. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1244. __func__,
  1245. obj->read_domains, obj->write_domain,
  1246. dev->invalidate_domains, dev->flush_domains);
  1247. #endif
  1248. return 0;
  1249. }
  1250. /**
  1251. * Set the read/write domain on a range of the object.
  1252. *
  1253. * Currently only implemented for CPU reads, otherwise drops to normal
  1254. * i915_gem_object_set_domain().
  1255. */
  1256. static int
  1257. i915_gem_object_set_domain_range(struct drm_gem_object *obj,
  1258. uint64_t offset,
  1259. uint64_t size,
  1260. uint32_t read_domains,
  1261. uint32_t write_domain)
  1262. {
  1263. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1264. int ret, i;
  1265. if (obj->read_domains & I915_GEM_DOMAIN_CPU)
  1266. return 0;
  1267. if (read_domains != I915_GEM_DOMAIN_CPU ||
  1268. write_domain != 0)
  1269. return i915_gem_object_set_domain(obj,
  1270. read_domains, write_domain);
  1271. /* Wait on any GPU rendering to the object to be flushed. */
  1272. ret = i915_gem_object_wait_rendering(obj);
  1273. if (ret)
  1274. return ret;
  1275. if (obj_priv->page_cpu_valid == NULL) {
  1276. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1277. DRM_MEM_DRIVER);
  1278. }
  1279. /* Flush the cache on any pages that are still invalid from the CPU's
  1280. * perspective.
  1281. */
  1282. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; i++) {
  1283. if (obj_priv->page_cpu_valid[i])
  1284. continue;
  1285. drm_clflush_pages(obj_priv->page_list + i, 1);
  1286. obj_priv->page_cpu_valid[i] = 1;
  1287. }
  1288. return 0;
  1289. }
  1290. /**
  1291. * Once all of the objects have been set in the proper domain,
  1292. * perform the necessary flush and invalidate operations.
  1293. *
  1294. * Returns the write domains flushed, for use in flush tracking.
  1295. */
  1296. static uint32_t
  1297. i915_gem_dev_set_domain(struct drm_device *dev)
  1298. {
  1299. uint32_t flush_domains = dev->flush_domains;
  1300. /*
  1301. * Now that all the buffers are synced to the proper domains,
  1302. * flush and invalidate the collected domains
  1303. */
  1304. if (dev->invalidate_domains | dev->flush_domains) {
  1305. #if WATCH_EXEC
  1306. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  1307. __func__,
  1308. dev->invalidate_domains,
  1309. dev->flush_domains);
  1310. #endif
  1311. i915_gem_flush(dev,
  1312. dev->invalidate_domains,
  1313. dev->flush_domains);
  1314. dev->invalidate_domains = 0;
  1315. dev->flush_domains = 0;
  1316. }
  1317. return flush_domains;
  1318. }
  1319. /**
  1320. * Pin an object to the GTT and evaluate the relocations landing in it.
  1321. */
  1322. static int
  1323. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1324. struct drm_file *file_priv,
  1325. struct drm_i915_gem_exec_object *entry)
  1326. {
  1327. struct drm_device *dev = obj->dev;
  1328. drm_i915_private_t *dev_priv = dev->dev_private;
  1329. struct drm_i915_gem_relocation_entry reloc;
  1330. struct drm_i915_gem_relocation_entry __user *relocs;
  1331. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1332. int i, ret;
  1333. void __iomem *reloc_page;
  1334. /* Choose the GTT offset for our buffer and put it there. */
  1335. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1336. if (ret)
  1337. return ret;
  1338. entry->offset = obj_priv->gtt_offset;
  1339. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1340. (uintptr_t) entry->relocs_ptr;
  1341. /* Apply the relocations, using the GTT aperture to avoid cache
  1342. * flushing requirements.
  1343. */
  1344. for (i = 0; i < entry->relocation_count; i++) {
  1345. struct drm_gem_object *target_obj;
  1346. struct drm_i915_gem_object *target_obj_priv;
  1347. uint32_t reloc_val, reloc_offset;
  1348. uint32_t __iomem *reloc_entry;
  1349. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1350. if (ret != 0) {
  1351. i915_gem_object_unpin(obj);
  1352. return ret;
  1353. }
  1354. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1355. reloc.target_handle);
  1356. if (target_obj == NULL) {
  1357. i915_gem_object_unpin(obj);
  1358. return -EBADF;
  1359. }
  1360. target_obj_priv = target_obj->driver_private;
  1361. /* The target buffer should have appeared before us in the
  1362. * exec_object list, so it should have a GTT space bound by now.
  1363. */
  1364. if (target_obj_priv->gtt_space == NULL) {
  1365. DRM_ERROR("No GTT space found for object %d\n",
  1366. reloc.target_handle);
  1367. drm_gem_object_unreference(target_obj);
  1368. i915_gem_object_unpin(obj);
  1369. return -EINVAL;
  1370. }
  1371. if (reloc.offset > obj->size - 4) {
  1372. DRM_ERROR("Relocation beyond object bounds: "
  1373. "obj %p target %d offset %d size %d.\n",
  1374. obj, reloc.target_handle,
  1375. (int) reloc.offset, (int) obj->size);
  1376. drm_gem_object_unreference(target_obj);
  1377. i915_gem_object_unpin(obj);
  1378. return -EINVAL;
  1379. }
  1380. if (reloc.offset & 3) {
  1381. DRM_ERROR("Relocation not 4-byte aligned: "
  1382. "obj %p target %d offset %d.\n",
  1383. obj, reloc.target_handle,
  1384. (int) reloc.offset);
  1385. drm_gem_object_unreference(target_obj);
  1386. i915_gem_object_unpin(obj);
  1387. return -EINVAL;
  1388. }
  1389. if (reloc.write_domain && target_obj->pending_write_domain &&
  1390. reloc.write_domain != target_obj->pending_write_domain) {
  1391. DRM_ERROR("Write domain conflict: "
  1392. "obj %p target %d offset %d "
  1393. "new %08x old %08x\n",
  1394. obj, reloc.target_handle,
  1395. (int) reloc.offset,
  1396. reloc.write_domain,
  1397. target_obj->pending_write_domain);
  1398. drm_gem_object_unreference(target_obj);
  1399. i915_gem_object_unpin(obj);
  1400. return -EINVAL;
  1401. }
  1402. #if WATCH_RELOC
  1403. DRM_INFO("%s: obj %p offset %08x target %d "
  1404. "read %08x write %08x gtt %08x "
  1405. "presumed %08x delta %08x\n",
  1406. __func__,
  1407. obj,
  1408. (int) reloc.offset,
  1409. (int) reloc.target_handle,
  1410. (int) reloc.read_domains,
  1411. (int) reloc.write_domain,
  1412. (int) target_obj_priv->gtt_offset,
  1413. (int) reloc.presumed_offset,
  1414. reloc.delta);
  1415. #endif
  1416. target_obj->pending_read_domains |= reloc.read_domains;
  1417. target_obj->pending_write_domain |= reloc.write_domain;
  1418. /* If the relocation already has the right value in it, no
  1419. * more work needs to be done.
  1420. */
  1421. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1422. drm_gem_object_unreference(target_obj);
  1423. continue;
  1424. }
  1425. /* Now that we're going to actually write some data in,
  1426. * make sure that any rendering using this buffer's contents
  1427. * is completed.
  1428. */
  1429. i915_gem_object_wait_rendering(obj);
  1430. /* As we're writing through the gtt, flush
  1431. * any CPU writes before we write the relocations
  1432. */
  1433. if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
  1434. i915_gem_clflush_object(obj);
  1435. drm_agp_chipset_flush(dev);
  1436. obj->write_domain = 0;
  1437. }
  1438. /* Map the page containing the relocation we're going to
  1439. * perform.
  1440. */
  1441. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1442. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  1443. (reloc_offset &
  1444. ~(PAGE_SIZE - 1)));
  1445. reloc_entry = (uint32_t __iomem *)(reloc_page +
  1446. (reloc_offset & (PAGE_SIZE - 1)));
  1447. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  1448. #if WATCH_BUF
  1449. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  1450. obj, (unsigned int) reloc.offset,
  1451. readl(reloc_entry), reloc_val);
  1452. #endif
  1453. writel(reloc_val, reloc_entry);
  1454. io_mapping_unmap_atomic(reloc_page);
  1455. /* Write the updated presumed offset for this entry back out
  1456. * to the user.
  1457. */
  1458. reloc.presumed_offset = target_obj_priv->gtt_offset;
  1459. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  1460. if (ret != 0) {
  1461. drm_gem_object_unreference(target_obj);
  1462. i915_gem_object_unpin(obj);
  1463. return ret;
  1464. }
  1465. drm_gem_object_unreference(target_obj);
  1466. }
  1467. #if WATCH_BUF
  1468. if (0)
  1469. i915_gem_dump_object(obj, 128, __func__, ~0);
  1470. #endif
  1471. return 0;
  1472. }
  1473. /** Dispatch a batchbuffer to the ring
  1474. */
  1475. static int
  1476. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  1477. struct drm_i915_gem_execbuffer *exec,
  1478. uint64_t exec_offset)
  1479. {
  1480. drm_i915_private_t *dev_priv = dev->dev_private;
  1481. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  1482. (uintptr_t) exec->cliprects_ptr;
  1483. int nbox = exec->num_cliprects;
  1484. int i = 0, count;
  1485. uint32_t exec_start, exec_len;
  1486. RING_LOCALS;
  1487. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  1488. exec_len = (uint32_t) exec->batch_len;
  1489. if ((exec_start | exec_len) & 0x7) {
  1490. DRM_ERROR("alignment\n");
  1491. return -EINVAL;
  1492. }
  1493. if (!exec_start)
  1494. return -EINVAL;
  1495. count = nbox ? nbox : 1;
  1496. for (i = 0; i < count; i++) {
  1497. if (i < nbox) {
  1498. int ret = i915_emit_box(dev, boxes, i,
  1499. exec->DR1, exec->DR4);
  1500. if (ret)
  1501. return ret;
  1502. }
  1503. if (IS_I830(dev) || IS_845G(dev)) {
  1504. BEGIN_LP_RING(4);
  1505. OUT_RING(MI_BATCH_BUFFER);
  1506. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1507. OUT_RING(exec_start + exec_len - 4);
  1508. OUT_RING(0);
  1509. ADVANCE_LP_RING();
  1510. } else {
  1511. BEGIN_LP_RING(2);
  1512. if (IS_I965G(dev)) {
  1513. OUT_RING(MI_BATCH_BUFFER_START |
  1514. (2 << 6) |
  1515. MI_BATCH_NON_SECURE_I965);
  1516. OUT_RING(exec_start);
  1517. } else {
  1518. OUT_RING(MI_BATCH_BUFFER_START |
  1519. (2 << 6));
  1520. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1521. }
  1522. ADVANCE_LP_RING();
  1523. }
  1524. }
  1525. /* XXX breadcrumb */
  1526. return 0;
  1527. }
  1528. /* Throttle our rendering by waiting until the ring has completed our requests
  1529. * emitted over 20 msec ago.
  1530. *
  1531. * This should get us reasonable parallelism between CPU and GPU but also
  1532. * relatively low latency when blocking on a particular request to finish.
  1533. */
  1534. static int
  1535. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  1536. {
  1537. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1538. int ret = 0;
  1539. uint32_t seqno;
  1540. mutex_lock(&dev->struct_mutex);
  1541. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  1542. i915_file_priv->mm.last_gem_throttle_seqno =
  1543. i915_file_priv->mm.last_gem_seqno;
  1544. if (seqno)
  1545. ret = i915_wait_request(dev, seqno);
  1546. mutex_unlock(&dev->struct_mutex);
  1547. return ret;
  1548. }
  1549. int
  1550. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1551. struct drm_file *file_priv)
  1552. {
  1553. drm_i915_private_t *dev_priv = dev->dev_private;
  1554. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1555. struct drm_i915_gem_execbuffer *args = data;
  1556. struct drm_i915_gem_exec_object *exec_list = NULL;
  1557. struct drm_gem_object **object_list = NULL;
  1558. struct drm_gem_object *batch_obj;
  1559. int ret, i, pinned = 0;
  1560. uint64_t exec_offset;
  1561. uint32_t seqno, flush_domains;
  1562. #if WATCH_EXEC
  1563. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1564. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1565. #endif
  1566. if (args->buffer_count < 1) {
  1567. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1568. return -EINVAL;
  1569. }
  1570. /* Copy in the exec list from userland */
  1571. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  1572. DRM_MEM_DRIVER);
  1573. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  1574. DRM_MEM_DRIVER);
  1575. if (exec_list == NULL || object_list == NULL) {
  1576. DRM_ERROR("Failed to allocate exec or object list "
  1577. "for %d buffers\n",
  1578. args->buffer_count);
  1579. ret = -ENOMEM;
  1580. goto pre_mutex_err;
  1581. }
  1582. ret = copy_from_user(exec_list,
  1583. (struct drm_i915_relocation_entry __user *)
  1584. (uintptr_t) args->buffers_ptr,
  1585. sizeof(*exec_list) * args->buffer_count);
  1586. if (ret != 0) {
  1587. DRM_ERROR("copy %d exec entries failed %d\n",
  1588. args->buffer_count, ret);
  1589. goto pre_mutex_err;
  1590. }
  1591. mutex_lock(&dev->struct_mutex);
  1592. i915_verify_inactive(dev, __FILE__, __LINE__);
  1593. if (dev_priv->mm.wedged) {
  1594. DRM_ERROR("Execbuf while wedged\n");
  1595. mutex_unlock(&dev->struct_mutex);
  1596. return -EIO;
  1597. }
  1598. if (dev_priv->mm.suspended) {
  1599. DRM_ERROR("Execbuf while VT-switched.\n");
  1600. mutex_unlock(&dev->struct_mutex);
  1601. return -EBUSY;
  1602. }
  1603. /* Zero the gloabl flush/invalidate flags. These
  1604. * will be modified as each object is bound to the
  1605. * gtt
  1606. */
  1607. dev->invalidate_domains = 0;
  1608. dev->flush_domains = 0;
  1609. /* Look up object handles and perform the relocations */
  1610. for (i = 0; i < args->buffer_count; i++) {
  1611. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  1612. exec_list[i].handle);
  1613. if (object_list[i] == NULL) {
  1614. DRM_ERROR("Invalid object handle %d at index %d\n",
  1615. exec_list[i].handle, i);
  1616. ret = -EBADF;
  1617. goto err;
  1618. }
  1619. object_list[i]->pending_read_domains = 0;
  1620. object_list[i]->pending_write_domain = 0;
  1621. ret = i915_gem_object_pin_and_relocate(object_list[i],
  1622. file_priv,
  1623. &exec_list[i]);
  1624. if (ret) {
  1625. DRM_ERROR("object bind and relocate failed %d\n", ret);
  1626. goto err;
  1627. }
  1628. pinned = i + 1;
  1629. }
  1630. /* Set the pending read domains for the batch buffer to COMMAND */
  1631. batch_obj = object_list[args->buffer_count-1];
  1632. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1633. batch_obj->pending_write_domain = 0;
  1634. i915_verify_inactive(dev, __FILE__, __LINE__);
  1635. for (i = 0; i < args->buffer_count; i++) {
  1636. struct drm_gem_object *obj = object_list[i];
  1637. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1638. if (obj_priv->gtt_space == NULL) {
  1639. /* We evicted the buffer in the process of validating
  1640. * our set of buffers in. We could try to recover by
  1641. * kicking them everything out and trying again from
  1642. * the start.
  1643. */
  1644. ret = -ENOMEM;
  1645. goto err;
  1646. }
  1647. /* make sure all previous memory operations have passed */
  1648. ret = i915_gem_object_set_domain(obj,
  1649. obj->pending_read_domains,
  1650. obj->pending_write_domain);
  1651. if (ret)
  1652. goto err;
  1653. }
  1654. i915_verify_inactive(dev, __FILE__, __LINE__);
  1655. /* Flush/invalidate caches and chipset buffer */
  1656. flush_domains = i915_gem_dev_set_domain(dev);
  1657. i915_verify_inactive(dev, __FILE__, __LINE__);
  1658. #if WATCH_COHERENCY
  1659. for (i = 0; i < args->buffer_count; i++) {
  1660. i915_gem_object_check_coherency(object_list[i],
  1661. exec_list[i].handle);
  1662. }
  1663. #endif
  1664. exec_offset = exec_list[args->buffer_count - 1].offset;
  1665. #if WATCH_EXEC
  1666. i915_gem_dump_object(object_list[args->buffer_count - 1],
  1667. args->batch_len,
  1668. __func__,
  1669. ~0);
  1670. #endif
  1671. (void)i915_add_request(dev, flush_domains);
  1672. /* Exec the batchbuffer */
  1673. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  1674. if (ret) {
  1675. DRM_ERROR("dispatch failed %d\n", ret);
  1676. goto err;
  1677. }
  1678. /*
  1679. * Ensure that the commands in the batch buffer are
  1680. * finished before the interrupt fires
  1681. */
  1682. flush_domains = i915_retire_commands(dev);
  1683. i915_verify_inactive(dev, __FILE__, __LINE__);
  1684. /*
  1685. * Get a seqno representing the execution of the current buffer,
  1686. * which we can wait on. We would like to mitigate these interrupts,
  1687. * likely by only creating seqnos occasionally (so that we have
  1688. * *some* interrupts representing completion of buffers that we can
  1689. * wait on when trying to clear up gtt space).
  1690. */
  1691. seqno = i915_add_request(dev, flush_domains);
  1692. BUG_ON(seqno == 0);
  1693. i915_file_priv->mm.last_gem_seqno = seqno;
  1694. for (i = 0; i < args->buffer_count; i++) {
  1695. struct drm_gem_object *obj = object_list[i];
  1696. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1697. i915_gem_object_move_to_active(obj);
  1698. obj_priv->last_rendering_seqno = seqno;
  1699. #if WATCH_LRU
  1700. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  1701. #endif
  1702. }
  1703. #if WATCH_LRU
  1704. i915_dump_lru(dev, __func__);
  1705. #endif
  1706. i915_verify_inactive(dev, __FILE__, __LINE__);
  1707. /* Copy the new buffer offsets back to the user's exec list. */
  1708. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1709. (uintptr_t) args->buffers_ptr,
  1710. exec_list,
  1711. sizeof(*exec_list) * args->buffer_count);
  1712. if (ret)
  1713. DRM_ERROR("failed to copy %d exec entries "
  1714. "back to user (%d)\n",
  1715. args->buffer_count, ret);
  1716. err:
  1717. if (object_list != NULL) {
  1718. for (i = 0; i < pinned; i++)
  1719. i915_gem_object_unpin(object_list[i]);
  1720. for (i = 0; i < args->buffer_count; i++)
  1721. drm_gem_object_unreference(object_list[i]);
  1722. }
  1723. mutex_unlock(&dev->struct_mutex);
  1724. pre_mutex_err:
  1725. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  1726. DRM_MEM_DRIVER);
  1727. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  1728. DRM_MEM_DRIVER);
  1729. return ret;
  1730. }
  1731. int
  1732. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  1733. {
  1734. struct drm_device *dev = obj->dev;
  1735. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1736. int ret;
  1737. i915_verify_inactive(dev, __FILE__, __LINE__);
  1738. if (obj_priv->gtt_space == NULL) {
  1739. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  1740. if (ret != 0) {
  1741. DRM_ERROR("Failure to bind: %d", ret);
  1742. return ret;
  1743. }
  1744. }
  1745. obj_priv->pin_count++;
  1746. /* If the object is not active and not pending a flush,
  1747. * remove it from the inactive list
  1748. */
  1749. if (obj_priv->pin_count == 1) {
  1750. atomic_inc(&dev->pin_count);
  1751. atomic_add(obj->size, &dev->pin_memory);
  1752. if (!obj_priv->active &&
  1753. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1754. I915_GEM_DOMAIN_GTT)) == 0 &&
  1755. !list_empty(&obj_priv->list))
  1756. list_del_init(&obj_priv->list);
  1757. }
  1758. i915_verify_inactive(dev, __FILE__, __LINE__);
  1759. return 0;
  1760. }
  1761. void
  1762. i915_gem_object_unpin(struct drm_gem_object *obj)
  1763. {
  1764. struct drm_device *dev = obj->dev;
  1765. drm_i915_private_t *dev_priv = dev->dev_private;
  1766. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1767. i915_verify_inactive(dev, __FILE__, __LINE__);
  1768. obj_priv->pin_count--;
  1769. BUG_ON(obj_priv->pin_count < 0);
  1770. BUG_ON(obj_priv->gtt_space == NULL);
  1771. /* If the object is no longer pinned, and is
  1772. * neither active nor being flushed, then stick it on
  1773. * the inactive list
  1774. */
  1775. if (obj_priv->pin_count == 0) {
  1776. if (!obj_priv->active &&
  1777. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1778. I915_GEM_DOMAIN_GTT)) == 0)
  1779. list_move_tail(&obj_priv->list,
  1780. &dev_priv->mm.inactive_list);
  1781. atomic_dec(&dev->pin_count);
  1782. atomic_sub(obj->size, &dev->pin_memory);
  1783. }
  1784. i915_verify_inactive(dev, __FILE__, __LINE__);
  1785. }
  1786. int
  1787. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  1788. struct drm_file *file_priv)
  1789. {
  1790. struct drm_i915_gem_pin *args = data;
  1791. struct drm_gem_object *obj;
  1792. struct drm_i915_gem_object *obj_priv;
  1793. int ret;
  1794. mutex_lock(&dev->struct_mutex);
  1795. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1796. if (obj == NULL) {
  1797. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  1798. args->handle);
  1799. mutex_unlock(&dev->struct_mutex);
  1800. return -EBADF;
  1801. }
  1802. obj_priv = obj->driver_private;
  1803. ret = i915_gem_object_pin(obj, args->alignment);
  1804. if (ret != 0) {
  1805. drm_gem_object_unreference(obj);
  1806. mutex_unlock(&dev->struct_mutex);
  1807. return ret;
  1808. }
  1809. /* XXX - flush the CPU caches for pinned objects
  1810. * as the X server doesn't manage domains yet
  1811. */
  1812. if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
  1813. i915_gem_clflush_object(obj);
  1814. drm_agp_chipset_flush(dev);
  1815. obj->write_domain = 0;
  1816. }
  1817. args->offset = obj_priv->gtt_offset;
  1818. drm_gem_object_unreference(obj);
  1819. mutex_unlock(&dev->struct_mutex);
  1820. return 0;
  1821. }
  1822. int
  1823. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  1824. struct drm_file *file_priv)
  1825. {
  1826. struct drm_i915_gem_pin *args = data;
  1827. struct drm_gem_object *obj;
  1828. mutex_lock(&dev->struct_mutex);
  1829. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1830. if (obj == NULL) {
  1831. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  1832. args->handle);
  1833. mutex_unlock(&dev->struct_mutex);
  1834. return -EBADF;
  1835. }
  1836. i915_gem_object_unpin(obj);
  1837. drm_gem_object_unreference(obj);
  1838. mutex_unlock(&dev->struct_mutex);
  1839. return 0;
  1840. }
  1841. int
  1842. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  1843. struct drm_file *file_priv)
  1844. {
  1845. struct drm_i915_gem_busy *args = data;
  1846. struct drm_gem_object *obj;
  1847. struct drm_i915_gem_object *obj_priv;
  1848. mutex_lock(&dev->struct_mutex);
  1849. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1850. if (obj == NULL) {
  1851. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  1852. args->handle);
  1853. mutex_unlock(&dev->struct_mutex);
  1854. return -EBADF;
  1855. }
  1856. obj_priv = obj->driver_private;
  1857. args->busy = obj_priv->active;
  1858. drm_gem_object_unreference(obj);
  1859. mutex_unlock(&dev->struct_mutex);
  1860. return 0;
  1861. }
  1862. int
  1863. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  1864. struct drm_file *file_priv)
  1865. {
  1866. return i915_gem_ring_throttle(dev, file_priv);
  1867. }
  1868. int i915_gem_init_object(struct drm_gem_object *obj)
  1869. {
  1870. struct drm_i915_gem_object *obj_priv;
  1871. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  1872. if (obj_priv == NULL)
  1873. return -ENOMEM;
  1874. /*
  1875. * We've just allocated pages from the kernel,
  1876. * so they've just been written by the CPU with
  1877. * zeros. They'll need to be clflushed before we
  1878. * use them with the GPU.
  1879. */
  1880. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1881. obj->read_domains = I915_GEM_DOMAIN_CPU;
  1882. obj_priv->agp_type = AGP_USER_MEMORY;
  1883. obj->driver_private = obj_priv;
  1884. obj_priv->obj = obj;
  1885. INIT_LIST_HEAD(&obj_priv->list);
  1886. return 0;
  1887. }
  1888. void i915_gem_free_object(struct drm_gem_object *obj)
  1889. {
  1890. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1891. while (obj_priv->pin_count > 0)
  1892. i915_gem_object_unpin(obj);
  1893. i915_gem_object_unbind(obj);
  1894. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  1895. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  1896. }
  1897. static int
  1898. i915_gem_set_domain(struct drm_gem_object *obj,
  1899. struct drm_file *file_priv,
  1900. uint32_t read_domains,
  1901. uint32_t write_domain)
  1902. {
  1903. struct drm_device *dev = obj->dev;
  1904. int ret;
  1905. uint32_t flush_domains;
  1906. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1907. ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
  1908. if (ret)
  1909. return ret;
  1910. flush_domains = i915_gem_dev_set_domain(obj->dev);
  1911. if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
  1912. (void) i915_add_request(dev, flush_domains);
  1913. return 0;
  1914. }
  1915. /** Unbinds all objects that are on the given buffer list. */
  1916. static int
  1917. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  1918. {
  1919. struct drm_gem_object *obj;
  1920. struct drm_i915_gem_object *obj_priv;
  1921. int ret;
  1922. while (!list_empty(head)) {
  1923. obj_priv = list_first_entry(head,
  1924. struct drm_i915_gem_object,
  1925. list);
  1926. obj = obj_priv->obj;
  1927. if (obj_priv->pin_count != 0) {
  1928. DRM_ERROR("Pinned object in unbind list\n");
  1929. mutex_unlock(&dev->struct_mutex);
  1930. return -EINVAL;
  1931. }
  1932. ret = i915_gem_object_unbind(obj);
  1933. if (ret != 0) {
  1934. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  1935. ret);
  1936. mutex_unlock(&dev->struct_mutex);
  1937. return ret;
  1938. }
  1939. }
  1940. return 0;
  1941. }
  1942. static int
  1943. i915_gem_idle(struct drm_device *dev)
  1944. {
  1945. drm_i915_private_t *dev_priv = dev->dev_private;
  1946. uint32_t seqno, cur_seqno, last_seqno;
  1947. int stuck, ret;
  1948. mutex_lock(&dev->struct_mutex);
  1949. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  1950. mutex_unlock(&dev->struct_mutex);
  1951. return 0;
  1952. }
  1953. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  1954. * We need to replace this with a semaphore, or something.
  1955. */
  1956. dev_priv->mm.suspended = 1;
  1957. /* Cancel the retire work handler, wait for it to finish if running
  1958. */
  1959. mutex_unlock(&dev->struct_mutex);
  1960. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  1961. mutex_lock(&dev->struct_mutex);
  1962. i915_kernel_lost_context(dev);
  1963. /* Flush the GPU along with all non-CPU write domains
  1964. */
  1965. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  1966. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1967. seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
  1968. I915_GEM_DOMAIN_GTT));
  1969. if (seqno == 0) {
  1970. mutex_unlock(&dev->struct_mutex);
  1971. return -ENOMEM;
  1972. }
  1973. dev_priv->mm.waiting_gem_seqno = seqno;
  1974. last_seqno = 0;
  1975. stuck = 0;
  1976. for (;;) {
  1977. cur_seqno = i915_get_gem_seqno(dev);
  1978. if (i915_seqno_passed(cur_seqno, seqno))
  1979. break;
  1980. if (last_seqno == cur_seqno) {
  1981. if (stuck++ > 100) {
  1982. DRM_ERROR("hardware wedged\n");
  1983. dev_priv->mm.wedged = 1;
  1984. DRM_WAKEUP(&dev_priv->irq_queue);
  1985. break;
  1986. }
  1987. }
  1988. msleep(10);
  1989. last_seqno = cur_seqno;
  1990. }
  1991. dev_priv->mm.waiting_gem_seqno = 0;
  1992. i915_gem_retire_requests(dev);
  1993. /* Active and flushing should now be empty as we've
  1994. * waited for a sequence higher than any pending execbuffer
  1995. */
  1996. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  1997. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1998. /* Request should now be empty as we've also waited
  1999. * for the last request in the list
  2000. */
  2001. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2002. /* Move all buffers out of the GTT. */
  2003. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2004. if (ret) {
  2005. mutex_unlock(&dev->struct_mutex);
  2006. return ret;
  2007. }
  2008. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2009. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2010. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2011. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2012. i915_gem_cleanup_ringbuffer(dev);
  2013. mutex_unlock(&dev->struct_mutex);
  2014. return 0;
  2015. }
  2016. static int
  2017. i915_gem_init_hws(struct drm_device *dev)
  2018. {
  2019. drm_i915_private_t *dev_priv = dev->dev_private;
  2020. struct drm_gem_object *obj;
  2021. struct drm_i915_gem_object *obj_priv;
  2022. int ret;
  2023. /* If we need a physical address for the status page, it's already
  2024. * initialized at driver load time.
  2025. */
  2026. if (!I915_NEED_GFX_HWS(dev))
  2027. return 0;
  2028. obj = drm_gem_object_alloc(dev, 4096);
  2029. if (obj == NULL) {
  2030. DRM_ERROR("Failed to allocate status page\n");
  2031. return -ENOMEM;
  2032. }
  2033. obj_priv = obj->driver_private;
  2034. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2035. ret = i915_gem_object_pin(obj, 4096);
  2036. if (ret != 0) {
  2037. drm_gem_object_unreference(obj);
  2038. return ret;
  2039. }
  2040. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2041. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2042. if (dev_priv->hw_status_page == NULL) {
  2043. DRM_ERROR("Failed to map status page.\n");
  2044. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2045. drm_gem_object_unreference(obj);
  2046. return -EINVAL;
  2047. }
  2048. dev_priv->hws_obj = obj;
  2049. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2050. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2051. I915_READ(HWS_PGA); /* posting read */
  2052. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2053. return 0;
  2054. }
  2055. static int
  2056. i915_gem_init_ringbuffer(struct drm_device *dev)
  2057. {
  2058. drm_i915_private_t *dev_priv = dev->dev_private;
  2059. struct drm_gem_object *obj;
  2060. struct drm_i915_gem_object *obj_priv;
  2061. int ret;
  2062. u32 head;
  2063. ret = i915_gem_init_hws(dev);
  2064. if (ret != 0)
  2065. return ret;
  2066. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2067. if (obj == NULL) {
  2068. DRM_ERROR("Failed to allocate ringbuffer\n");
  2069. return -ENOMEM;
  2070. }
  2071. obj_priv = obj->driver_private;
  2072. ret = i915_gem_object_pin(obj, 4096);
  2073. if (ret != 0) {
  2074. drm_gem_object_unreference(obj);
  2075. return ret;
  2076. }
  2077. /* Set up the kernel mapping for the ring. */
  2078. dev_priv->ring.Size = obj->size;
  2079. dev_priv->ring.tail_mask = obj->size - 1;
  2080. dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
  2081. dev_priv->ring.map.size = obj->size;
  2082. dev_priv->ring.map.type = 0;
  2083. dev_priv->ring.map.flags = 0;
  2084. dev_priv->ring.map.mtrr = 0;
  2085. drm_core_ioremap_wc(&dev_priv->ring.map, dev);
  2086. if (dev_priv->ring.map.handle == NULL) {
  2087. DRM_ERROR("Failed to map ringbuffer.\n");
  2088. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2089. drm_gem_object_unreference(obj);
  2090. return -EINVAL;
  2091. }
  2092. dev_priv->ring.ring_obj = obj;
  2093. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  2094. /* Stop the ring if it's running. */
  2095. I915_WRITE(PRB0_CTL, 0);
  2096. I915_WRITE(PRB0_TAIL, 0);
  2097. I915_WRITE(PRB0_HEAD, 0);
  2098. /* Initialize the ring. */
  2099. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2100. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2101. /* G45 ring initialization fails to reset head to zero */
  2102. if (head != 0) {
  2103. DRM_ERROR("Ring head not reset to zero "
  2104. "ctl %08x head %08x tail %08x start %08x\n",
  2105. I915_READ(PRB0_CTL),
  2106. I915_READ(PRB0_HEAD),
  2107. I915_READ(PRB0_TAIL),
  2108. I915_READ(PRB0_START));
  2109. I915_WRITE(PRB0_HEAD, 0);
  2110. DRM_ERROR("Ring head forced to zero "
  2111. "ctl %08x head %08x tail %08x start %08x\n",
  2112. I915_READ(PRB0_CTL),
  2113. I915_READ(PRB0_HEAD),
  2114. I915_READ(PRB0_TAIL),
  2115. I915_READ(PRB0_START));
  2116. }
  2117. I915_WRITE(PRB0_CTL,
  2118. ((obj->size - 4096) & RING_NR_PAGES) |
  2119. RING_NO_REPORT |
  2120. RING_VALID);
  2121. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2122. /* If the head is still not zero, the ring is dead */
  2123. if (head != 0) {
  2124. DRM_ERROR("Ring initialization failed "
  2125. "ctl %08x head %08x tail %08x start %08x\n",
  2126. I915_READ(PRB0_CTL),
  2127. I915_READ(PRB0_HEAD),
  2128. I915_READ(PRB0_TAIL),
  2129. I915_READ(PRB0_START));
  2130. return -EIO;
  2131. }
  2132. /* Update our cache of the ring state */
  2133. i915_kernel_lost_context(dev);
  2134. return 0;
  2135. }
  2136. static void
  2137. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2138. {
  2139. drm_i915_private_t *dev_priv = dev->dev_private;
  2140. if (dev_priv->ring.ring_obj == NULL)
  2141. return;
  2142. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2143. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2144. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2145. dev_priv->ring.ring_obj = NULL;
  2146. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2147. if (dev_priv->hws_obj != NULL) {
  2148. struct drm_gem_object *obj = dev_priv->hws_obj;
  2149. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2150. kunmap(obj_priv->page_list[0]);
  2151. i915_gem_object_unpin(obj);
  2152. drm_gem_object_unreference(obj);
  2153. dev_priv->hws_obj = NULL;
  2154. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2155. dev_priv->hw_status_page = NULL;
  2156. /* Write high address into HWS_PGA when disabling. */
  2157. I915_WRITE(HWS_PGA, 0x1ffff000);
  2158. }
  2159. }
  2160. int
  2161. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2162. struct drm_file *file_priv)
  2163. {
  2164. drm_i915_private_t *dev_priv = dev->dev_private;
  2165. int ret;
  2166. if (dev_priv->mm.wedged) {
  2167. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2168. dev_priv->mm.wedged = 0;
  2169. }
  2170. ret = i915_gem_init_ringbuffer(dev);
  2171. if (ret != 0)
  2172. return ret;
  2173. dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
  2174. dev->agp->agp_info.aper_size
  2175. * 1024 * 1024);
  2176. mutex_lock(&dev->struct_mutex);
  2177. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2178. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2179. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2180. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2181. dev_priv->mm.suspended = 0;
  2182. mutex_unlock(&dev->struct_mutex);
  2183. drm_irq_install(dev);
  2184. return 0;
  2185. }
  2186. int
  2187. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2188. struct drm_file *file_priv)
  2189. {
  2190. drm_i915_private_t *dev_priv = dev->dev_private;
  2191. int ret;
  2192. ret = i915_gem_idle(dev);
  2193. drm_irq_uninstall(dev);
  2194. io_mapping_free(dev_priv->mm.gtt_mapping);
  2195. return ret;
  2196. }
  2197. void
  2198. i915_gem_lastclose(struct drm_device *dev)
  2199. {
  2200. int ret;
  2201. ret = i915_gem_idle(dev);
  2202. if (ret)
  2203. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2204. }
  2205. void
  2206. i915_gem_load(struct drm_device *dev)
  2207. {
  2208. drm_i915_private_t *dev_priv = dev->dev_private;
  2209. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2210. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2211. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2212. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2213. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2214. i915_gem_retire_work_handler);
  2215. dev_priv->mm.next_gem_seqno = 1;
  2216. i915_gem_detect_bit_6_swizzle(dev);
  2217. }