i915_gem.c 65 KB

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