i915_gem.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315
  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 "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/shmem_fs.h>
  34. #include <linux/slab.h>
  35. #include <linux/swap.h>
  36. #include <linux/pci.h>
  37. static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  39. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  40. static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
  41. bool write);
  42. static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
  43. uint64_t offset,
  44. uint64_t size);
  45. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
  46. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  47. unsigned alignment,
  48. bool map_and_fenceable);
  49. static void i915_gem_clear_fence_reg(struct drm_device *dev,
  50. struct drm_i915_fence_reg *reg);
  51. static int i915_gem_phys_pwrite(struct drm_device *dev,
  52. struct drm_i915_gem_object *obj,
  53. struct drm_i915_gem_pwrite *args,
  54. struct drm_file *file);
  55. static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
  56. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  57. struct shrink_control *sc);
  58. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  59. /* some bookkeeping */
  60. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  61. size_t size)
  62. {
  63. dev_priv->mm.object_count++;
  64. dev_priv->mm.object_memory += size;
  65. }
  66. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  67. size_t size)
  68. {
  69. dev_priv->mm.object_count--;
  70. dev_priv->mm.object_memory -= size;
  71. }
  72. static int
  73. i915_gem_wait_for_error(struct drm_device *dev)
  74. {
  75. struct drm_i915_private *dev_priv = dev->dev_private;
  76. struct completion *x = &dev_priv->error_completion;
  77. unsigned long flags;
  78. int ret;
  79. if (!atomic_read(&dev_priv->mm.wedged))
  80. return 0;
  81. ret = wait_for_completion_interruptible(x);
  82. if (ret)
  83. return ret;
  84. if (atomic_read(&dev_priv->mm.wedged)) {
  85. /* GPU is hung, bump the completion count to account for
  86. * the token we just consumed so that we never hit zero and
  87. * end up waiting upon a subsequent completion event that
  88. * will never happen.
  89. */
  90. spin_lock_irqsave(&x->wait.lock, flags);
  91. x->done++;
  92. spin_unlock_irqrestore(&x->wait.lock, flags);
  93. }
  94. return 0;
  95. }
  96. int i915_mutex_lock_interruptible(struct drm_device *dev)
  97. {
  98. int ret;
  99. ret = i915_gem_wait_for_error(dev);
  100. if (ret)
  101. return ret;
  102. ret = mutex_lock_interruptible(&dev->struct_mutex);
  103. if (ret)
  104. return ret;
  105. WARN_ON(i915_verify_lists(dev));
  106. return 0;
  107. }
  108. static inline bool
  109. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  110. {
  111. return obj->gtt_space && !obj->active && obj->pin_count == 0;
  112. }
  113. void i915_gem_do_init(struct drm_device *dev,
  114. unsigned long start,
  115. unsigned long mappable_end,
  116. unsigned long end)
  117. {
  118. drm_i915_private_t *dev_priv = dev->dev_private;
  119. drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
  120. dev_priv->mm.gtt_start = start;
  121. dev_priv->mm.gtt_mappable_end = mappable_end;
  122. dev_priv->mm.gtt_end = end;
  123. dev_priv->mm.gtt_total = end - start;
  124. dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
  125. /* Take over this portion of the GTT */
  126. intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
  127. }
  128. int
  129. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  130. struct drm_file *file)
  131. {
  132. struct drm_i915_gem_init *args = data;
  133. if (args->gtt_start >= args->gtt_end ||
  134. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  135. return -EINVAL;
  136. mutex_lock(&dev->struct_mutex);
  137. i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
  138. mutex_unlock(&dev->struct_mutex);
  139. return 0;
  140. }
  141. int
  142. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  143. struct drm_file *file)
  144. {
  145. struct drm_i915_private *dev_priv = dev->dev_private;
  146. struct drm_i915_gem_get_aperture *args = data;
  147. struct drm_i915_gem_object *obj;
  148. size_t pinned;
  149. if (!(dev->driver->driver_features & DRIVER_GEM))
  150. return -ENODEV;
  151. pinned = 0;
  152. mutex_lock(&dev->struct_mutex);
  153. list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
  154. pinned += obj->gtt_space->size;
  155. mutex_unlock(&dev->struct_mutex);
  156. args->aper_size = dev_priv->mm.gtt_total;
  157. args->aper_available_size = args->aper_size - pinned;
  158. return 0;
  159. }
  160. static int
  161. i915_gem_create(struct drm_file *file,
  162. struct drm_device *dev,
  163. uint64_t size,
  164. uint32_t *handle_p)
  165. {
  166. struct drm_i915_gem_object *obj;
  167. int ret;
  168. u32 handle;
  169. size = roundup(size, PAGE_SIZE);
  170. if (size == 0)
  171. return -EINVAL;
  172. /* Allocate the new object */
  173. obj = i915_gem_alloc_object(dev, size);
  174. if (obj == NULL)
  175. return -ENOMEM;
  176. ret = drm_gem_handle_create(file, &obj->base, &handle);
  177. if (ret) {
  178. drm_gem_object_release(&obj->base);
  179. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  180. kfree(obj);
  181. return ret;
  182. }
  183. /* drop reference from allocate - handle holds it now */
  184. drm_gem_object_unreference(&obj->base);
  185. trace_i915_gem_object_create(obj);
  186. *handle_p = handle;
  187. return 0;
  188. }
  189. int
  190. i915_gem_dumb_create(struct drm_file *file,
  191. struct drm_device *dev,
  192. struct drm_mode_create_dumb *args)
  193. {
  194. /* have to work out size/pitch and return them */
  195. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  196. args->size = args->pitch * args->height;
  197. return i915_gem_create(file, dev,
  198. args->size, &args->handle);
  199. }
  200. int i915_gem_dumb_destroy(struct drm_file *file,
  201. struct drm_device *dev,
  202. uint32_t handle)
  203. {
  204. return drm_gem_handle_delete(file, handle);
  205. }
  206. /**
  207. * Creates a new mm object and returns a handle to it.
  208. */
  209. int
  210. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  211. struct drm_file *file)
  212. {
  213. struct drm_i915_gem_create *args = data;
  214. return i915_gem_create(file, dev,
  215. args->size, &args->handle);
  216. }
  217. static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
  218. {
  219. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  220. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  221. obj->tiling_mode != I915_TILING_NONE;
  222. }
  223. /**
  224. * This is the fast shmem pread path, which attempts to copy_from_user directly
  225. * from the backing pages of the object to the user's address space. On a
  226. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  227. */
  228. static int
  229. i915_gem_shmem_pread_fast(struct drm_device *dev,
  230. struct drm_i915_gem_object *obj,
  231. struct drm_i915_gem_pread *args,
  232. struct drm_file *file)
  233. {
  234. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  235. ssize_t remain;
  236. loff_t offset;
  237. char __user *user_data;
  238. int page_offset, page_length;
  239. user_data = (char __user *) (uintptr_t) args->data_ptr;
  240. remain = args->size;
  241. offset = args->offset;
  242. while (remain > 0) {
  243. struct page *page;
  244. char *vaddr;
  245. int ret;
  246. /* Operation in this page
  247. *
  248. * page_offset = offset within page
  249. * page_length = bytes to copy for this page
  250. */
  251. page_offset = offset_in_page(offset);
  252. page_length = remain;
  253. if ((page_offset + remain) > PAGE_SIZE)
  254. page_length = PAGE_SIZE - page_offset;
  255. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  256. if (IS_ERR(page))
  257. return PTR_ERR(page);
  258. vaddr = kmap_atomic(page);
  259. ret = __copy_to_user_inatomic(user_data,
  260. vaddr + page_offset,
  261. page_length);
  262. kunmap_atomic(vaddr);
  263. mark_page_accessed(page);
  264. page_cache_release(page);
  265. if (ret)
  266. return -EFAULT;
  267. remain -= page_length;
  268. user_data += page_length;
  269. offset += page_length;
  270. }
  271. return 0;
  272. }
  273. static inline int
  274. __copy_to_user_swizzled(char __user *cpu_vaddr,
  275. const char *gpu_vaddr, int gpu_offset,
  276. int length)
  277. {
  278. int ret, cpu_offset = 0;
  279. while (length > 0) {
  280. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  281. int this_length = min(cacheline_end - gpu_offset, length);
  282. int swizzled_gpu_offset = gpu_offset ^ 64;
  283. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  284. gpu_vaddr + swizzled_gpu_offset,
  285. this_length);
  286. if (ret)
  287. return ret + length;
  288. cpu_offset += this_length;
  289. gpu_offset += this_length;
  290. length -= this_length;
  291. }
  292. return 0;
  293. }
  294. static inline int
  295. __copy_from_user_swizzled(char __user *gpu_vaddr, int gpu_offset,
  296. const char *cpu_vaddr,
  297. int length)
  298. {
  299. int ret, cpu_offset = 0;
  300. while (length > 0) {
  301. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  302. int this_length = min(cacheline_end - gpu_offset, length);
  303. int swizzled_gpu_offset = gpu_offset ^ 64;
  304. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  305. cpu_vaddr + cpu_offset,
  306. this_length);
  307. if (ret)
  308. return ret + length;
  309. cpu_offset += this_length;
  310. gpu_offset += this_length;
  311. length -= this_length;
  312. }
  313. return 0;
  314. }
  315. /**
  316. * This is the fallback shmem pread path, which allocates temporary storage
  317. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  318. * can copy out of the object's backing pages while holding the struct mutex
  319. * and not take page faults.
  320. */
  321. static int
  322. i915_gem_shmem_pread_slow(struct drm_device *dev,
  323. struct drm_i915_gem_object *obj,
  324. struct drm_i915_gem_pread *args,
  325. struct drm_file *file)
  326. {
  327. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  328. char __user *user_data;
  329. ssize_t remain;
  330. loff_t offset;
  331. int shmem_page_offset, page_length, ret;
  332. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  333. user_data = (char __user *) (uintptr_t) args->data_ptr;
  334. remain = args->size;
  335. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  336. offset = args->offset;
  337. mutex_unlock(&dev->struct_mutex);
  338. while (remain > 0) {
  339. struct page *page;
  340. char *vaddr;
  341. /* Operation in this page
  342. *
  343. * shmem_page_offset = offset within page in shmem file
  344. * page_length = bytes to copy for this page
  345. */
  346. shmem_page_offset = offset_in_page(offset);
  347. page_length = remain;
  348. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  349. page_length = PAGE_SIZE - shmem_page_offset;
  350. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  351. if (IS_ERR(page)) {
  352. ret = PTR_ERR(page);
  353. goto out;
  354. }
  355. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  356. (page_to_phys(page) & (1 << 17)) != 0;
  357. vaddr = kmap(page);
  358. if (page_do_bit17_swizzling)
  359. ret = __copy_to_user_swizzled(user_data,
  360. vaddr, shmem_page_offset,
  361. page_length);
  362. else
  363. ret = __copy_to_user(user_data,
  364. vaddr + shmem_page_offset,
  365. page_length);
  366. kunmap(page);
  367. mark_page_accessed(page);
  368. page_cache_release(page);
  369. if (ret) {
  370. ret = -EFAULT;
  371. goto out;
  372. }
  373. remain -= page_length;
  374. user_data += page_length;
  375. offset += page_length;
  376. }
  377. out:
  378. mutex_lock(&dev->struct_mutex);
  379. /* Fixup: Kill any reinstated backing storage pages */
  380. if (obj->madv == __I915_MADV_PURGED)
  381. i915_gem_object_truncate(obj);
  382. return ret;
  383. }
  384. /**
  385. * Reads data from the object referenced by handle.
  386. *
  387. * On error, the contents of *data are undefined.
  388. */
  389. int
  390. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  391. struct drm_file *file)
  392. {
  393. struct drm_i915_gem_pread *args = data;
  394. struct drm_i915_gem_object *obj;
  395. int ret = 0;
  396. if (args->size == 0)
  397. return 0;
  398. if (!access_ok(VERIFY_WRITE,
  399. (char __user *)(uintptr_t)args->data_ptr,
  400. args->size))
  401. return -EFAULT;
  402. ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
  403. args->size);
  404. if (ret)
  405. return -EFAULT;
  406. ret = i915_mutex_lock_interruptible(dev);
  407. if (ret)
  408. return ret;
  409. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  410. if (&obj->base == NULL) {
  411. ret = -ENOENT;
  412. goto unlock;
  413. }
  414. /* Bounds check source. */
  415. if (args->offset > obj->base.size ||
  416. args->size > obj->base.size - args->offset) {
  417. ret = -EINVAL;
  418. goto out;
  419. }
  420. trace_i915_gem_object_pread(obj, args->offset, args->size);
  421. ret = i915_gem_object_set_cpu_read_domain_range(obj,
  422. args->offset,
  423. args->size);
  424. if (ret)
  425. goto out;
  426. ret = -EFAULT;
  427. if (!i915_gem_object_needs_bit17_swizzle(obj))
  428. ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
  429. if (ret == -EFAULT)
  430. ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
  431. out:
  432. drm_gem_object_unreference(&obj->base);
  433. unlock:
  434. mutex_unlock(&dev->struct_mutex);
  435. return ret;
  436. }
  437. /* This is the fast write path which cannot handle
  438. * page faults in the source data
  439. */
  440. static inline int
  441. fast_user_write(struct io_mapping *mapping,
  442. loff_t page_base, int page_offset,
  443. char __user *user_data,
  444. int length)
  445. {
  446. char *vaddr_atomic;
  447. unsigned long unwritten;
  448. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  449. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  450. user_data, length);
  451. io_mapping_unmap_atomic(vaddr_atomic);
  452. return unwritten;
  453. }
  454. /* Here's the write path which can sleep for
  455. * page faults
  456. */
  457. static inline void
  458. slow_kernel_write(struct io_mapping *mapping,
  459. loff_t gtt_base, int gtt_offset,
  460. struct page *user_page, int user_offset,
  461. int length)
  462. {
  463. char __iomem *dst_vaddr;
  464. char *src_vaddr;
  465. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  466. src_vaddr = kmap(user_page);
  467. memcpy_toio(dst_vaddr + gtt_offset,
  468. src_vaddr + user_offset,
  469. length);
  470. kunmap(user_page);
  471. io_mapping_unmap(dst_vaddr);
  472. }
  473. /**
  474. * This is the fast pwrite path, where we copy the data directly from the
  475. * user into the GTT, uncached.
  476. */
  477. static int
  478. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  479. struct drm_i915_gem_object *obj,
  480. struct drm_i915_gem_pwrite *args,
  481. struct drm_file *file)
  482. {
  483. drm_i915_private_t *dev_priv = dev->dev_private;
  484. ssize_t remain;
  485. loff_t offset, page_base;
  486. char __user *user_data;
  487. int page_offset, page_length;
  488. user_data = (char __user *) (uintptr_t) args->data_ptr;
  489. remain = args->size;
  490. offset = obj->gtt_offset + args->offset;
  491. while (remain > 0) {
  492. /* Operation in this page
  493. *
  494. * page_base = page offset within aperture
  495. * page_offset = offset within page
  496. * page_length = bytes to copy for this page
  497. */
  498. page_base = offset & PAGE_MASK;
  499. page_offset = offset_in_page(offset);
  500. page_length = remain;
  501. if ((page_offset + remain) > PAGE_SIZE)
  502. page_length = PAGE_SIZE - page_offset;
  503. /* If we get a fault while copying data, then (presumably) our
  504. * source page isn't available. Return the error and we'll
  505. * retry in the slow path.
  506. */
  507. if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
  508. page_offset, user_data, page_length))
  509. return -EFAULT;
  510. remain -= page_length;
  511. user_data += page_length;
  512. offset += page_length;
  513. }
  514. return 0;
  515. }
  516. /**
  517. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  518. * the memory and maps it using kmap_atomic for copying.
  519. *
  520. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  521. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  522. */
  523. static int
  524. i915_gem_gtt_pwrite_slow(struct drm_device *dev,
  525. struct drm_i915_gem_object *obj,
  526. struct drm_i915_gem_pwrite *args,
  527. struct drm_file *file)
  528. {
  529. drm_i915_private_t *dev_priv = dev->dev_private;
  530. ssize_t remain;
  531. loff_t gtt_page_base, offset;
  532. loff_t first_data_page, last_data_page, num_pages;
  533. loff_t pinned_pages, i;
  534. struct page **user_pages;
  535. struct mm_struct *mm = current->mm;
  536. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  537. int ret;
  538. uint64_t data_ptr = args->data_ptr;
  539. remain = args->size;
  540. /* Pin the user pages containing the data. We can't fault while
  541. * holding the struct mutex, and all of the pwrite implementations
  542. * want to hold it while dereferencing the user data.
  543. */
  544. first_data_page = data_ptr / PAGE_SIZE;
  545. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  546. num_pages = last_data_page - first_data_page + 1;
  547. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  548. if (user_pages == NULL)
  549. return -ENOMEM;
  550. mutex_unlock(&dev->struct_mutex);
  551. down_read(&mm->mmap_sem);
  552. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  553. num_pages, 0, 0, user_pages, NULL);
  554. up_read(&mm->mmap_sem);
  555. mutex_lock(&dev->struct_mutex);
  556. if (pinned_pages < num_pages) {
  557. ret = -EFAULT;
  558. goto out_unpin_pages;
  559. }
  560. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  561. if (ret)
  562. goto out_unpin_pages;
  563. ret = i915_gem_object_put_fence(obj);
  564. if (ret)
  565. goto out_unpin_pages;
  566. offset = obj->gtt_offset + args->offset;
  567. while (remain > 0) {
  568. /* Operation in this page
  569. *
  570. * gtt_page_base = page offset within aperture
  571. * gtt_page_offset = offset within page in aperture
  572. * data_page_index = page number in get_user_pages return
  573. * data_page_offset = offset with data_page_index page.
  574. * page_length = bytes to copy for this page
  575. */
  576. gtt_page_base = offset & PAGE_MASK;
  577. gtt_page_offset = offset_in_page(offset);
  578. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  579. data_page_offset = offset_in_page(data_ptr);
  580. page_length = remain;
  581. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  582. page_length = PAGE_SIZE - gtt_page_offset;
  583. if ((data_page_offset + page_length) > PAGE_SIZE)
  584. page_length = PAGE_SIZE - data_page_offset;
  585. slow_kernel_write(dev_priv->mm.gtt_mapping,
  586. gtt_page_base, gtt_page_offset,
  587. user_pages[data_page_index],
  588. data_page_offset,
  589. page_length);
  590. remain -= page_length;
  591. offset += page_length;
  592. data_ptr += page_length;
  593. }
  594. out_unpin_pages:
  595. for (i = 0; i < pinned_pages; i++)
  596. page_cache_release(user_pages[i]);
  597. drm_free_large(user_pages);
  598. return ret;
  599. }
  600. /**
  601. * This is the fast shmem pwrite path, which attempts to directly
  602. * copy_from_user into the kmapped pages backing the object.
  603. */
  604. static int
  605. i915_gem_shmem_pwrite_fast(struct drm_device *dev,
  606. struct drm_i915_gem_object *obj,
  607. struct drm_i915_gem_pwrite *args,
  608. struct drm_file *file)
  609. {
  610. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  611. ssize_t remain;
  612. loff_t offset;
  613. char __user *user_data;
  614. int page_offset, page_length;
  615. user_data = (char __user *) (uintptr_t) args->data_ptr;
  616. remain = args->size;
  617. offset = args->offset;
  618. obj->dirty = 1;
  619. while (remain > 0) {
  620. struct page *page;
  621. char *vaddr;
  622. int ret;
  623. /* Operation in this page
  624. *
  625. * page_offset = offset within page
  626. * page_length = bytes to copy for this page
  627. */
  628. page_offset = offset_in_page(offset);
  629. page_length = remain;
  630. if ((page_offset + remain) > PAGE_SIZE)
  631. page_length = PAGE_SIZE - page_offset;
  632. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  633. if (IS_ERR(page))
  634. return PTR_ERR(page);
  635. vaddr = kmap_atomic(page);
  636. ret = __copy_from_user_inatomic(vaddr + page_offset,
  637. user_data,
  638. page_length);
  639. kunmap_atomic(vaddr);
  640. set_page_dirty(page);
  641. mark_page_accessed(page);
  642. page_cache_release(page);
  643. /* If we get a fault while copying data, then (presumably) our
  644. * source page isn't available. Return the error and we'll
  645. * retry in the slow path.
  646. */
  647. if (ret)
  648. return -EFAULT;
  649. remain -= page_length;
  650. user_data += page_length;
  651. offset += page_length;
  652. }
  653. return 0;
  654. }
  655. /**
  656. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  657. * the memory and maps it using kmap_atomic for copying.
  658. *
  659. * This avoids taking mmap_sem for faulting on the user's address while the
  660. * struct_mutex is held.
  661. */
  662. static int
  663. i915_gem_shmem_pwrite_slow(struct drm_device *dev,
  664. struct drm_i915_gem_object *obj,
  665. struct drm_i915_gem_pwrite *args,
  666. struct drm_file *file)
  667. {
  668. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  669. ssize_t remain;
  670. loff_t offset;
  671. char __user *user_data;
  672. int shmem_page_offset, page_length, ret;
  673. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  674. user_data = (char __user *) (uintptr_t) args->data_ptr;
  675. remain = args->size;
  676. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  677. offset = args->offset;
  678. obj->dirty = 1;
  679. mutex_unlock(&dev->struct_mutex);
  680. while (remain > 0) {
  681. struct page *page;
  682. char *vaddr;
  683. /* Operation in this page
  684. *
  685. * shmem_page_offset = offset within page in shmem file
  686. * page_length = bytes to copy for this page
  687. */
  688. shmem_page_offset = offset_in_page(offset);
  689. page_length = remain;
  690. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  691. page_length = PAGE_SIZE - shmem_page_offset;
  692. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  693. if (IS_ERR(page)) {
  694. ret = PTR_ERR(page);
  695. goto out;
  696. }
  697. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  698. (page_to_phys(page) & (1 << 17)) != 0;
  699. vaddr = kmap(page);
  700. if (page_do_bit17_swizzling)
  701. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  702. user_data,
  703. page_length);
  704. else
  705. ret = __copy_from_user(vaddr + shmem_page_offset,
  706. user_data,
  707. page_length);
  708. kunmap(page);
  709. set_page_dirty(page);
  710. mark_page_accessed(page);
  711. page_cache_release(page);
  712. if (ret) {
  713. ret = -EFAULT;
  714. goto out;
  715. }
  716. remain -= page_length;
  717. user_data += page_length;
  718. offset += page_length;
  719. }
  720. out:
  721. mutex_lock(&dev->struct_mutex);
  722. /* Fixup: Kill any reinstated backing storage pages */
  723. if (obj->madv == __I915_MADV_PURGED)
  724. i915_gem_object_truncate(obj);
  725. /* and flush dirty cachelines in case the object isn't in the cpu write
  726. * domain anymore. */
  727. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  728. i915_gem_clflush_object(obj);
  729. intel_gtt_chipset_flush();
  730. }
  731. return ret;
  732. }
  733. /**
  734. * Writes data to the object referenced by handle.
  735. *
  736. * On error, the contents of the buffer that were to be modified are undefined.
  737. */
  738. int
  739. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  740. struct drm_file *file)
  741. {
  742. struct drm_i915_gem_pwrite *args = data;
  743. struct drm_i915_gem_object *obj;
  744. int ret;
  745. if (args->size == 0)
  746. return 0;
  747. if (!access_ok(VERIFY_READ,
  748. (char __user *)(uintptr_t)args->data_ptr,
  749. args->size))
  750. return -EFAULT;
  751. ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
  752. args->size);
  753. if (ret)
  754. return -EFAULT;
  755. ret = i915_mutex_lock_interruptible(dev);
  756. if (ret)
  757. return ret;
  758. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  759. if (&obj->base == NULL) {
  760. ret = -ENOENT;
  761. goto unlock;
  762. }
  763. /* Bounds check destination. */
  764. if (args->offset > obj->base.size ||
  765. args->size > obj->base.size - args->offset) {
  766. ret = -EINVAL;
  767. goto out;
  768. }
  769. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  770. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  771. * it would end up going through the fenced access, and we'll get
  772. * different detiling behavior between reading and writing.
  773. * pread/pwrite currently are reading and writing from the CPU
  774. * perspective, requiring manual detiling by the client.
  775. */
  776. if (obj->phys_obj) {
  777. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  778. goto out;
  779. }
  780. if (obj->gtt_space &&
  781. obj->tiling_mode == I915_TILING_NONE &&
  782. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  783. ret = i915_gem_object_pin(obj, 0, true);
  784. if (ret)
  785. goto out;
  786. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  787. if (ret)
  788. goto out_unpin;
  789. ret = i915_gem_object_put_fence(obj);
  790. if (ret)
  791. goto out_unpin;
  792. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  793. if (ret == -EFAULT)
  794. ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
  795. out_unpin:
  796. i915_gem_object_unpin(obj);
  797. if (ret != -EFAULT)
  798. goto out;
  799. /* Fall through to the shmfs paths because the gtt paths might
  800. * fail with non-page-backed user pointers (e.g. gtt mappings
  801. * when moving data between textures). */
  802. }
  803. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  804. if (ret)
  805. goto out;
  806. ret = -EFAULT;
  807. if (!i915_gem_object_needs_bit17_swizzle(obj))
  808. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
  809. if (ret == -EFAULT)
  810. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
  811. out:
  812. drm_gem_object_unreference(&obj->base);
  813. unlock:
  814. mutex_unlock(&dev->struct_mutex);
  815. return ret;
  816. }
  817. /**
  818. * Called when user space prepares to use an object with the CPU, either
  819. * through the mmap ioctl's mapping or a GTT mapping.
  820. */
  821. int
  822. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  823. struct drm_file *file)
  824. {
  825. struct drm_i915_gem_set_domain *args = data;
  826. struct drm_i915_gem_object *obj;
  827. uint32_t read_domains = args->read_domains;
  828. uint32_t write_domain = args->write_domain;
  829. int ret;
  830. if (!(dev->driver->driver_features & DRIVER_GEM))
  831. return -ENODEV;
  832. /* Only handle setting domains to types used by the CPU. */
  833. if (write_domain & I915_GEM_GPU_DOMAINS)
  834. return -EINVAL;
  835. if (read_domains & I915_GEM_GPU_DOMAINS)
  836. return -EINVAL;
  837. /* Having something in the write domain implies it's in the read
  838. * domain, and only that read domain. Enforce that in the request.
  839. */
  840. if (write_domain != 0 && read_domains != write_domain)
  841. return -EINVAL;
  842. ret = i915_mutex_lock_interruptible(dev);
  843. if (ret)
  844. return ret;
  845. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  846. if (&obj->base == NULL) {
  847. ret = -ENOENT;
  848. goto unlock;
  849. }
  850. if (read_domains & I915_GEM_DOMAIN_GTT) {
  851. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  852. /* Silently promote "you're not bound, there was nothing to do"
  853. * to success, since the client was just asking us to
  854. * make sure everything was done.
  855. */
  856. if (ret == -EINVAL)
  857. ret = 0;
  858. } else {
  859. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  860. }
  861. drm_gem_object_unreference(&obj->base);
  862. unlock:
  863. mutex_unlock(&dev->struct_mutex);
  864. return ret;
  865. }
  866. /**
  867. * Called when user space has done writes to this buffer
  868. */
  869. int
  870. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  871. struct drm_file *file)
  872. {
  873. struct drm_i915_gem_sw_finish *args = data;
  874. struct drm_i915_gem_object *obj;
  875. int ret = 0;
  876. if (!(dev->driver->driver_features & DRIVER_GEM))
  877. return -ENODEV;
  878. ret = i915_mutex_lock_interruptible(dev);
  879. if (ret)
  880. return ret;
  881. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  882. if (&obj->base == NULL) {
  883. ret = -ENOENT;
  884. goto unlock;
  885. }
  886. /* Pinned buffers may be scanout, so flush the cache */
  887. if (obj->pin_count)
  888. i915_gem_object_flush_cpu_write_domain(obj);
  889. drm_gem_object_unreference(&obj->base);
  890. unlock:
  891. mutex_unlock(&dev->struct_mutex);
  892. return ret;
  893. }
  894. /**
  895. * Maps the contents of an object, returning the address it is mapped
  896. * into.
  897. *
  898. * While the mapping holds a reference on the contents of the object, it doesn't
  899. * imply a ref on the object itself.
  900. */
  901. int
  902. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  903. struct drm_file *file)
  904. {
  905. struct drm_i915_gem_mmap *args = data;
  906. struct drm_gem_object *obj;
  907. unsigned long addr;
  908. if (!(dev->driver->driver_features & DRIVER_GEM))
  909. return -ENODEV;
  910. obj = drm_gem_object_lookup(dev, file, args->handle);
  911. if (obj == NULL)
  912. return -ENOENT;
  913. addr = vm_mmap(obj->filp, 0, args->size,
  914. PROT_READ | PROT_WRITE, MAP_SHARED,
  915. args->offset);
  916. drm_gem_object_unreference_unlocked(obj);
  917. if (IS_ERR((void *)addr))
  918. return addr;
  919. args->addr_ptr = (uint64_t) addr;
  920. return 0;
  921. }
  922. /**
  923. * i915_gem_fault - fault a page into the GTT
  924. * vma: VMA in question
  925. * vmf: fault info
  926. *
  927. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  928. * from userspace. The fault handler takes care of binding the object to
  929. * the GTT (if needed), allocating and programming a fence register (again,
  930. * only if needed based on whether the old reg is still valid or the object
  931. * is tiled) and inserting a new PTE into the faulting process.
  932. *
  933. * Note that the faulting process may involve evicting existing objects
  934. * from the GTT and/or fence registers to make room. So performance may
  935. * suffer if the GTT working set is large or there are few fence registers
  936. * left.
  937. */
  938. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  939. {
  940. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  941. struct drm_device *dev = obj->base.dev;
  942. drm_i915_private_t *dev_priv = dev->dev_private;
  943. pgoff_t page_offset;
  944. unsigned long pfn;
  945. int ret = 0;
  946. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  947. /* We don't use vmf->pgoff since that has the fake offset */
  948. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  949. PAGE_SHIFT;
  950. ret = i915_mutex_lock_interruptible(dev);
  951. if (ret)
  952. goto out;
  953. trace_i915_gem_object_fault(obj, page_offset, true, write);
  954. /* Now bind it into the GTT if needed */
  955. if (!obj->map_and_fenceable) {
  956. ret = i915_gem_object_unbind(obj);
  957. if (ret)
  958. goto unlock;
  959. }
  960. if (!obj->gtt_space) {
  961. ret = i915_gem_object_bind_to_gtt(obj, 0, true);
  962. if (ret)
  963. goto unlock;
  964. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  965. if (ret)
  966. goto unlock;
  967. }
  968. if (obj->tiling_mode == I915_TILING_NONE)
  969. ret = i915_gem_object_put_fence(obj);
  970. else
  971. ret = i915_gem_object_get_fence(obj, NULL);
  972. if (ret)
  973. goto unlock;
  974. if (i915_gem_object_is_inactive(obj))
  975. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  976. obj->fault_mappable = true;
  977. pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
  978. page_offset;
  979. /* Finally, remap it using the new GTT offset */
  980. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  981. unlock:
  982. mutex_unlock(&dev->struct_mutex);
  983. out:
  984. switch (ret) {
  985. case -EIO:
  986. case -EAGAIN:
  987. /* Give the error handler a chance to run and move the
  988. * objects off the GPU active list. Next time we service the
  989. * fault, we should be able to transition the page into the
  990. * GTT without touching the GPU (and so avoid further
  991. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  992. * with coherency, just lost writes.
  993. */
  994. set_need_resched();
  995. case 0:
  996. case -ERESTARTSYS:
  997. case -EINTR:
  998. case -EBUSY:
  999. /*
  1000. * EBUSY is ok: this just means that another thread
  1001. * already did the job.
  1002. */
  1003. return VM_FAULT_NOPAGE;
  1004. case -ENOMEM:
  1005. return VM_FAULT_OOM;
  1006. default:
  1007. return VM_FAULT_SIGBUS;
  1008. }
  1009. }
  1010. /**
  1011. * i915_gem_release_mmap - remove physical page mappings
  1012. * @obj: obj in question
  1013. *
  1014. * Preserve the reservation of the mmapping with the DRM core code, but
  1015. * relinquish ownership of the pages back to the system.
  1016. *
  1017. * It is vital that we remove the page mapping if we have mapped a tiled
  1018. * object through the GTT and then lose the fence register due to
  1019. * resource pressure. Similarly if the object has been moved out of the
  1020. * aperture, than pages mapped into userspace must be revoked. Removing the
  1021. * mapping will then trigger a page fault on the next user access, allowing
  1022. * fixup by i915_gem_fault().
  1023. */
  1024. void
  1025. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1026. {
  1027. if (!obj->fault_mappable)
  1028. return;
  1029. if (obj->base.dev->dev_mapping)
  1030. unmap_mapping_range(obj->base.dev->dev_mapping,
  1031. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1032. obj->base.size, 1);
  1033. obj->fault_mappable = false;
  1034. }
  1035. static uint32_t
  1036. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1037. {
  1038. uint32_t gtt_size;
  1039. if (INTEL_INFO(dev)->gen >= 4 ||
  1040. tiling_mode == I915_TILING_NONE)
  1041. return size;
  1042. /* Previous chips need a power-of-two fence region when tiling */
  1043. if (INTEL_INFO(dev)->gen == 3)
  1044. gtt_size = 1024*1024;
  1045. else
  1046. gtt_size = 512*1024;
  1047. while (gtt_size < size)
  1048. gtt_size <<= 1;
  1049. return gtt_size;
  1050. }
  1051. /**
  1052. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1053. * @obj: object to check
  1054. *
  1055. * Return the required GTT alignment for an object, taking into account
  1056. * potential fence register mapping.
  1057. */
  1058. static uint32_t
  1059. i915_gem_get_gtt_alignment(struct drm_device *dev,
  1060. uint32_t size,
  1061. int tiling_mode)
  1062. {
  1063. /*
  1064. * Minimum alignment is 4k (GTT page size), but might be greater
  1065. * if a fence register is needed for the object.
  1066. */
  1067. if (INTEL_INFO(dev)->gen >= 4 ||
  1068. tiling_mode == I915_TILING_NONE)
  1069. return 4096;
  1070. /*
  1071. * Previous chips need to be aligned to the size of the smallest
  1072. * fence register that can contain the object.
  1073. */
  1074. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1075. }
  1076. /**
  1077. * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
  1078. * unfenced object
  1079. * @dev: the device
  1080. * @size: size of the object
  1081. * @tiling_mode: tiling mode of the object
  1082. *
  1083. * Return the required GTT alignment for an object, only taking into account
  1084. * unfenced tiled surface requirements.
  1085. */
  1086. uint32_t
  1087. i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
  1088. uint32_t size,
  1089. int tiling_mode)
  1090. {
  1091. /*
  1092. * Minimum alignment is 4k (GTT page size) for sane hw.
  1093. */
  1094. if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
  1095. tiling_mode == I915_TILING_NONE)
  1096. return 4096;
  1097. /* Previous hardware however needs to be aligned to a power-of-two
  1098. * tile height. The simplest method for determining this is to reuse
  1099. * the power-of-tile object size.
  1100. */
  1101. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1102. }
  1103. int
  1104. i915_gem_mmap_gtt(struct drm_file *file,
  1105. struct drm_device *dev,
  1106. uint32_t handle,
  1107. uint64_t *offset)
  1108. {
  1109. struct drm_i915_private *dev_priv = dev->dev_private;
  1110. struct drm_i915_gem_object *obj;
  1111. int ret;
  1112. if (!(dev->driver->driver_features & DRIVER_GEM))
  1113. return -ENODEV;
  1114. ret = i915_mutex_lock_interruptible(dev);
  1115. if (ret)
  1116. return ret;
  1117. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1118. if (&obj->base == NULL) {
  1119. ret = -ENOENT;
  1120. goto unlock;
  1121. }
  1122. if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
  1123. ret = -E2BIG;
  1124. goto out;
  1125. }
  1126. if (obj->madv != I915_MADV_WILLNEED) {
  1127. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1128. ret = -EINVAL;
  1129. goto out;
  1130. }
  1131. if (!obj->base.map_list.map) {
  1132. ret = drm_gem_create_mmap_offset(&obj->base);
  1133. if (ret)
  1134. goto out;
  1135. }
  1136. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1137. out:
  1138. drm_gem_object_unreference(&obj->base);
  1139. unlock:
  1140. mutex_unlock(&dev->struct_mutex);
  1141. return ret;
  1142. }
  1143. /**
  1144. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1145. * @dev: DRM device
  1146. * @data: GTT mapping ioctl data
  1147. * @file: GEM object info
  1148. *
  1149. * Simply returns the fake offset to userspace so it can mmap it.
  1150. * The mmap call will end up in drm_gem_mmap(), which will set things
  1151. * up so we can get faults in the handler above.
  1152. *
  1153. * The fault handler will take care of binding the object into the GTT
  1154. * (since it may have been evicted to make room for something), allocating
  1155. * a fence register, and mapping the appropriate aperture address into
  1156. * userspace.
  1157. */
  1158. int
  1159. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1160. struct drm_file *file)
  1161. {
  1162. struct drm_i915_gem_mmap_gtt *args = data;
  1163. if (!(dev->driver->driver_features & DRIVER_GEM))
  1164. return -ENODEV;
  1165. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1166. }
  1167. static int
  1168. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
  1169. gfp_t gfpmask)
  1170. {
  1171. int page_count, i;
  1172. struct address_space *mapping;
  1173. struct inode *inode;
  1174. struct page *page;
  1175. /* Get the list of pages out of our struct file. They'll be pinned
  1176. * at this point until we release them.
  1177. */
  1178. page_count = obj->base.size / PAGE_SIZE;
  1179. BUG_ON(obj->pages != NULL);
  1180. obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
  1181. if (obj->pages == NULL)
  1182. return -ENOMEM;
  1183. inode = obj->base.filp->f_path.dentry->d_inode;
  1184. mapping = inode->i_mapping;
  1185. gfpmask |= mapping_gfp_mask(mapping);
  1186. for (i = 0; i < page_count; i++) {
  1187. page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
  1188. if (IS_ERR(page))
  1189. goto err_pages;
  1190. obj->pages[i] = page;
  1191. }
  1192. if (i915_gem_object_needs_bit17_swizzle(obj))
  1193. i915_gem_object_do_bit_17_swizzle(obj);
  1194. return 0;
  1195. err_pages:
  1196. while (i--)
  1197. page_cache_release(obj->pages[i]);
  1198. drm_free_large(obj->pages);
  1199. obj->pages = NULL;
  1200. return PTR_ERR(page);
  1201. }
  1202. static void
  1203. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1204. {
  1205. int page_count = obj->base.size / PAGE_SIZE;
  1206. int i;
  1207. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1208. if (i915_gem_object_needs_bit17_swizzle(obj))
  1209. i915_gem_object_save_bit_17_swizzle(obj);
  1210. if (obj->madv == I915_MADV_DONTNEED)
  1211. obj->dirty = 0;
  1212. for (i = 0; i < page_count; i++) {
  1213. if (obj->dirty)
  1214. set_page_dirty(obj->pages[i]);
  1215. if (obj->madv == I915_MADV_WILLNEED)
  1216. mark_page_accessed(obj->pages[i]);
  1217. page_cache_release(obj->pages[i]);
  1218. }
  1219. obj->dirty = 0;
  1220. drm_free_large(obj->pages);
  1221. obj->pages = NULL;
  1222. }
  1223. void
  1224. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1225. struct intel_ring_buffer *ring,
  1226. u32 seqno)
  1227. {
  1228. struct drm_device *dev = obj->base.dev;
  1229. struct drm_i915_private *dev_priv = dev->dev_private;
  1230. BUG_ON(ring == NULL);
  1231. obj->ring = ring;
  1232. /* Add a reference if we're newly entering the active list. */
  1233. if (!obj->active) {
  1234. drm_gem_object_reference(&obj->base);
  1235. obj->active = 1;
  1236. }
  1237. /* Move from whatever list we were on to the tail of execution. */
  1238. list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
  1239. list_move_tail(&obj->ring_list, &ring->active_list);
  1240. obj->last_rendering_seqno = seqno;
  1241. if (obj->fenced_gpu_access) {
  1242. obj->last_fenced_seqno = seqno;
  1243. obj->last_fenced_ring = ring;
  1244. /* Bump MRU to take account of the delayed flush */
  1245. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1246. struct drm_i915_fence_reg *reg;
  1247. reg = &dev_priv->fence_regs[obj->fence_reg];
  1248. list_move_tail(&reg->lru_list,
  1249. &dev_priv->mm.fence_list);
  1250. }
  1251. }
  1252. }
  1253. static void
  1254. i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
  1255. {
  1256. list_del_init(&obj->ring_list);
  1257. obj->last_rendering_seqno = 0;
  1258. obj->last_fenced_seqno = 0;
  1259. }
  1260. static void
  1261. i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
  1262. {
  1263. struct drm_device *dev = obj->base.dev;
  1264. drm_i915_private_t *dev_priv = dev->dev_private;
  1265. BUG_ON(!obj->active);
  1266. list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
  1267. i915_gem_object_move_off_active(obj);
  1268. }
  1269. static void
  1270. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1271. {
  1272. struct drm_device *dev = obj->base.dev;
  1273. struct drm_i915_private *dev_priv = dev->dev_private;
  1274. if (obj->pin_count != 0)
  1275. list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
  1276. else
  1277. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1278. BUG_ON(!list_empty(&obj->gpu_write_list));
  1279. BUG_ON(!obj->active);
  1280. obj->ring = NULL;
  1281. obj->last_fenced_ring = NULL;
  1282. i915_gem_object_move_off_active(obj);
  1283. obj->fenced_gpu_access = false;
  1284. obj->active = 0;
  1285. obj->pending_gpu_write = false;
  1286. drm_gem_object_unreference(&obj->base);
  1287. WARN_ON(i915_verify_lists(dev));
  1288. }
  1289. /* Immediately discard the backing storage */
  1290. static void
  1291. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1292. {
  1293. struct inode *inode;
  1294. /* Our goal here is to return as much of the memory as
  1295. * is possible back to the system as we are called from OOM.
  1296. * To do this we must instruct the shmfs to drop all of its
  1297. * backing pages, *now*.
  1298. */
  1299. inode = obj->base.filp->f_path.dentry->d_inode;
  1300. shmem_truncate_range(inode, 0, (loff_t)-1);
  1301. obj->madv = __I915_MADV_PURGED;
  1302. }
  1303. static inline int
  1304. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1305. {
  1306. return obj->madv == I915_MADV_DONTNEED;
  1307. }
  1308. static void
  1309. i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
  1310. uint32_t flush_domains)
  1311. {
  1312. struct drm_i915_gem_object *obj, *next;
  1313. list_for_each_entry_safe(obj, next,
  1314. &ring->gpu_write_list,
  1315. gpu_write_list) {
  1316. if (obj->base.write_domain & flush_domains) {
  1317. uint32_t old_write_domain = obj->base.write_domain;
  1318. obj->base.write_domain = 0;
  1319. list_del_init(&obj->gpu_write_list);
  1320. i915_gem_object_move_to_active(obj, ring,
  1321. i915_gem_next_request_seqno(ring));
  1322. trace_i915_gem_object_change_domain(obj,
  1323. obj->base.read_domains,
  1324. old_write_domain);
  1325. }
  1326. }
  1327. }
  1328. static u32
  1329. i915_gem_get_seqno(struct drm_device *dev)
  1330. {
  1331. drm_i915_private_t *dev_priv = dev->dev_private;
  1332. u32 seqno = dev_priv->next_seqno;
  1333. /* reserve 0 for non-seqno */
  1334. if (++dev_priv->next_seqno == 0)
  1335. dev_priv->next_seqno = 1;
  1336. return seqno;
  1337. }
  1338. u32
  1339. i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
  1340. {
  1341. if (ring->outstanding_lazy_request == 0)
  1342. ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
  1343. return ring->outstanding_lazy_request;
  1344. }
  1345. int
  1346. i915_add_request(struct intel_ring_buffer *ring,
  1347. struct drm_file *file,
  1348. struct drm_i915_gem_request *request)
  1349. {
  1350. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1351. uint32_t seqno;
  1352. u32 request_ring_position;
  1353. int was_empty;
  1354. int ret;
  1355. BUG_ON(request == NULL);
  1356. seqno = i915_gem_next_request_seqno(ring);
  1357. /* Record the position of the start of the request so that
  1358. * should we detect the updated seqno part-way through the
  1359. * GPU processing the request, we never over-estimate the
  1360. * position of the head.
  1361. */
  1362. request_ring_position = intel_ring_get_tail(ring);
  1363. ret = ring->add_request(ring, &seqno);
  1364. if (ret)
  1365. return ret;
  1366. trace_i915_gem_request_add(ring, seqno);
  1367. request->seqno = seqno;
  1368. request->ring = ring;
  1369. request->tail = request_ring_position;
  1370. request->emitted_jiffies = jiffies;
  1371. was_empty = list_empty(&ring->request_list);
  1372. list_add_tail(&request->list, &ring->request_list);
  1373. if (file) {
  1374. struct drm_i915_file_private *file_priv = file->driver_priv;
  1375. spin_lock(&file_priv->mm.lock);
  1376. request->file_priv = file_priv;
  1377. list_add_tail(&request->client_list,
  1378. &file_priv->mm.request_list);
  1379. spin_unlock(&file_priv->mm.lock);
  1380. }
  1381. ring->outstanding_lazy_request = 0;
  1382. if (!dev_priv->mm.suspended) {
  1383. if (i915_enable_hangcheck) {
  1384. mod_timer(&dev_priv->hangcheck_timer,
  1385. jiffies +
  1386. msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
  1387. }
  1388. if (was_empty)
  1389. queue_delayed_work(dev_priv->wq,
  1390. &dev_priv->mm.retire_work, HZ);
  1391. }
  1392. return 0;
  1393. }
  1394. static inline void
  1395. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1396. {
  1397. struct drm_i915_file_private *file_priv = request->file_priv;
  1398. if (!file_priv)
  1399. return;
  1400. spin_lock(&file_priv->mm.lock);
  1401. if (request->file_priv) {
  1402. list_del(&request->client_list);
  1403. request->file_priv = NULL;
  1404. }
  1405. spin_unlock(&file_priv->mm.lock);
  1406. }
  1407. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1408. struct intel_ring_buffer *ring)
  1409. {
  1410. while (!list_empty(&ring->request_list)) {
  1411. struct drm_i915_gem_request *request;
  1412. request = list_first_entry(&ring->request_list,
  1413. struct drm_i915_gem_request,
  1414. list);
  1415. list_del(&request->list);
  1416. i915_gem_request_remove_from_client(request);
  1417. kfree(request);
  1418. }
  1419. while (!list_empty(&ring->active_list)) {
  1420. struct drm_i915_gem_object *obj;
  1421. obj = list_first_entry(&ring->active_list,
  1422. struct drm_i915_gem_object,
  1423. ring_list);
  1424. obj->base.write_domain = 0;
  1425. list_del_init(&obj->gpu_write_list);
  1426. i915_gem_object_move_to_inactive(obj);
  1427. }
  1428. }
  1429. static void i915_gem_reset_fences(struct drm_device *dev)
  1430. {
  1431. struct drm_i915_private *dev_priv = dev->dev_private;
  1432. int i;
  1433. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1434. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1435. struct drm_i915_gem_object *obj = reg->obj;
  1436. if (!obj)
  1437. continue;
  1438. if (obj->tiling_mode)
  1439. i915_gem_release_mmap(obj);
  1440. reg->obj->fence_reg = I915_FENCE_REG_NONE;
  1441. reg->obj->fenced_gpu_access = false;
  1442. reg->obj->last_fenced_seqno = 0;
  1443. reg->obj->last_fenced_ring = NULL;
  1444. i915_gem_clear_fence_reg(dev, reg);
  1445. }
  1446. }
  1447. void i915_gem_reset(struct drm_device *dev)
  1448. {
  1449. struct drm_i915_private *dev_priv = dev->dev_private;
  1450. struct drm_i915_gem_object *obj;
  1451. int i;
  1452. for (i = 0; i < I915_NUM_RINGS; i++)
  1453. i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
  1454. /* Remove anything from the flushing lists. The GPU cache is likely
  1455. * to be lost on reset along with the data, so simply move the
  1456. * lost bo to the inactive list.
  1457. */
  1458. while (!list_empty(&dev_priv->mm.flushing_list)) {
  1459. obj = list_first_entry(&dev_priv->mm.flushing_list,
  1460. struct drm_i915_gem_object,
  1461. mm_list);
  1462. obj->base.write_domain = 0;
  1463. list_del_init(&obj->gpu_write_list);
  1464. i915_gem_object_move_to_inactive(obj);
  1465. }
  1466. /* Move everything out of the GPU domains to ensure we do any
  1467. * necessary invalidation upon reuse.
  1468. */
  1469. list_for_each_entry(obj,
  1470. &dev_priv->mm.inactive_list,
  1471. mm_list)
  1472. {
  1473. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1474. }
  1475. /* The fence registers are invalidated so clear them out */
  1476. i915_gem_reset_fences(dev);
  1477. }
  1478. /**
  1479. * This function clears the request list as sequence numbers are passed.
  1480. */
  1481. void
  1482. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1483. {
  1484. uint32_t seqno;
  1485. int i;
  1486. if (list_empty(&ring->request_list))
  1487. return;
  1488. WARN_ON(i915_verify_lists(ring->dev));
  1489. seqno = ring->get_seqno(ring);
  1490. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
  1491. if (seqno >= ring->sync_seqno[i])
  1492. ring->sync_seqno[i] = 0;
  1493. while (!list_empty(&ring->request_list)) {
  1494. struct drm_i915_gem_request *request;
  1495. request = list_first_entry(&ring->request_list,
  1496. struct drm_i915_gem_request,
  1497. list);
  1498. if (!i915_seqno_passed(seqno, request->seqno))
  1499. break;
  1500. trace_i915_gem_request_retire(ring, request->seqno);
  1501. /* We know the GPU must have read the request to have
  1502. * sent us the seqno + interrupt, so use the position
  1503. * of tail of the request to update the last known position
  1504. * of the GPU head.
  1505. */
  1506. ring->last_retired_head = request->tail;
  1507. list_del(&request->list);
  1508. i915_gem_request_remove_from_client(request);
  1509. kfree(request);
  1510. }
  1511. /* Move any buffers on the active list that are no longer referenced
  1512. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1513. */
  1514. while (!list_empty(&ring->active_list)) {
  1515. struct drm_i915_gem_object *obj;
  1516. obj = list_first_entry(&ring->active_list,
  1517. struct drm_i915_gem_object,
  1518. ring_list);
  1519. if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
  1520. break;
  1521. if (obj->base.write_domain != 0)
  1522. i915_gem_object_move_to_flushing(obj);
  1523. else
  1524. i915_gem_object_move_to_inactive(obj);
  1525. }
  1526. if (unlikely(ring->trace_irq_seqno &&
  1527. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1528. ring->irq_put(ring);
  1529. ring->trace_irq_seqno = 0;
  1530. }
  1531. WARN_ON(i915_verify_lists(ring->dev));
  1532. }
  1533. void
  1534. i915_gem_retire_requests(struct drm_device *dev)
  1535. {
  1536. drm_i915_private_t *dev_priv = dev->dev_private;
  1537. int i;
  1538. if (!list_empty(&dev_priv->mm.deferred_free_list)) {
  1539. struct drm_i915_gem_object *obj, *next;
  1540. /* We must be careful that during unbind() we do not
  1541. * accidentally infinitely recurse into retire requests.
  1542. * Currently:
  1543. * retire -> free -> unbind -> wait -> retire_ring
  1544. */
  1545. list_for_each_entry_safe(obj, next,
  1546. &dev_priv->mm.deferred_free_list,
  1547. mm_list)
  1548. i915_gem_free_object_tail(obj);
  1549. }
  1550. for (i = 0; i < I915_NUM_RINGS; i++)
  1551. i915_gem_retire_requests_ring(&dev_priv->ring[i]);
  1552. }
  1553. static void
  1554. i915_gem_retire_work_handler(struct work_struct *work)
  1555. {
  1556. drm_i915_private_t *dev_priv;
  1557. struct drm_device *dev;
  1558. bool idle;
  1559. int i;
  1560. dev_priv = container_of(work, drm_i915_private_t,
  1561. mm.retire_work.work);
  1562. dev = dev_priv->dev;
  1563. /* Come back later if the device is busy... */
  1564. if (!mutex_trylock(&dev->struct_mutex)) {
  1565. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1566. return;
  1567. }
  1568. i915_gem_retire_requests(dev);
  1569. /* Send a periodic flush down the ring so we don't hold onto GEM
  1570. * objects indefinitely.
  1571. */
  1572. idle = true;
  1573. for (i = 0; i < I915_NUM_RINGS; i++) {
  1574. struct intel_ring_buffer *ring = &dev_priv->ring[i];
  1575. if (!list_empty(&ring->gpu_write_list)) {
  1576. struct drm_i915_gem_request *request;
  1577. int ret;
  1578. ret = i915_gem_flush_ring(ring,
  1579. 0, I915_GEM_GPU_DOMAINS);
  1580. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1581. if (ret || request == NULL ||
  1582. i915_add_request(ring, NULL, request))
  1583. kfree(request);
  1584. }
  1585. idle &= list_empty(&ring->request_list);
  1586. }
  1587. if (!dev_priv->mm.suspended && !idle)
  1588. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1589. mutex_unlock(&dev->struct_mutex);
  1590. }
  1591. /**
  1592. * Waits for a sequence number to be signaled, and cleans up the
  1593. * request and object lists appropriately for that event.
  1594. */
  1595. int
  1596. i915_wait_request(struct intel_ring_buffer *ring,
  1597. uint32_t seqno,
  1598. bool do_retire)
  1599. {
  1600. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1601. u32 ier;
  1602. int ret = 0;
  1603. BUG_ON(seqno == 0);
  1604. if (atomic_read(&dev_priv->mm.wedged)) {
  1605. struct completion *x = &dev_priv->error_completion;
  1606. bool recovery_complete;
  1607. unsigned long flags;
  1608. /* Give the error handler a chance to run. */
  1609. spin_lock_irqsave(&x->wait.lock, flags);
  1610. recovery_complete = x->done > 0;
  1611. spin_unlock_irqrestore(&x->wait.lock, flags);
  1612. return recovery_complete ? -EIO : -EAGAIN;
  1613. }
  1614. if (seqno == ring->outstanding_lazy_request) {
  1615. struct drm_i915_gem_request *request;
  1616. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1617. if (request == NULL)
  1618. return -ENOMEM;
  1619. ret = i915_add_request(ring, NULL, request);
  1620. if (ret) {
  1621. kfree(request);
  1622. return ret;
  1623. }
  1624. seqno = request->seqno;
  1625. }
  1626. if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
  1627. if (HAS_PCH_SPLIT(ring->dev))
  1628. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1629. else
  1630. ier = I915_READ(IER);
  1631. if (!ier) {
  1632. DRM_ERROR("something (likely vbetool) disabled "
  1633. "interrupts, re-enabling\n");
  1634. ring->dev->driver->irq_preinstall(ring->dev);
  1635. ring->dev->driver->irq_postinstall(ring->dev);
  1636. }
  1637. trace_i915_gem_request_wait_begin(ring, seqno);
  1638. ring->waiting_seqno = seqno;
  1639. if (ring->irq_get(ring)) {
  1640. if (dev_priv->mm.interruptible)
  1641. ret = wait_event_interruptible(ring->irq_queue,
  1642. i915_seqno_passed(ring->get_seqno(ring), seqno)
  1643. || atomic_read(&dev_priv->mm.wedged));
  1644. else
  1645. wait_event(ring->irq_queue,
  1646. i915_seqno_passed(ring->get_seqno(ring), seqno)
  1647. || atomic_read(&dev_priv->mm.wedged));
  1648. ring->irq_put(ring);
  1649. } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
  1650. seqno) ||
  1651. atomic_read(&dev_priv->mm.wedged), 3000))
  1652. ret = -EBUSY;
  1653. ring->waiting_seqno = 0;
  1654. trace_i915_gem_request_wait_end(ring, seqno);
  1655. }
  1656. if (atomic_read(&dev_priv->mm.wedged))
  1657. ret = -EAGAIN;
  1658. /* Directly dispatch request retiring. While we have the work queue
  1659. * to handle this, the waiter on a request often wants an associated
  1660. * buffer to have made it to the inactive list, and we would need
  1661. * a separate wait queue to handle that.
  1662. */
  1663. if (ret == 0 && do_retire)
  1664. i915_gem_retire_requests_ring(ring);
  1665. return ret;
  1666. }
  1667. /**
  1668. * Ensures that all rendering to the object has completed and the object is
  1669. * safe to unbind from the GTT or access from the CPU.
  1670. */
  1671. int
  1672. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
  1673. {
  1674. int ret;
  1675. /* This function only exists to support waiting for existing rendering,
  1676. * not for emitting required flushes.
  1677. */
  1678. BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1679. /* If there is rendering queued on the buffer being evicted, wait for
  1680. * it.
  1681. */
  1682. if (obj->active) {
  1683. ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
  1684. true);
  1685. if (ret)
  1686. return ret;
  1687. }
  1688. return 0;
  1689. }
  1690. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  1691. {
  1692. u32 old_write_domain, old_read_domains;
  1693. /* Act a barrier for all accesses through the GTT */
  1694. mb();
  1695. /* Force a pagefault for domain tracking on next user access */
  1696. i915_gem_release_mmap(obj);
  1697. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  1698. return;
  1699. old_read_domains = obj->base.read_domains;
  1700. old_write_domain = obj->base.write_domain;
  1701. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  1702. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  1703. trace_i915_gem_object_change_domain(obj,
  1704. old_read_domains,
  1705. old_write_domain);
  1706. }
  1707. /**
  1708. * Unbinds an object from the GTT aperture.
  1709. */
  1710. int
  1711. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  1712. {
  1713. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  1714. int ret = 0;
  1715. if (obj->gtt_space == NULL)
  1716. return 0;
  1717. if (obj->pin_count != 0) {
  1718. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1719. return -EINVAL;
  1720. }
  1721. ret = i915_gem_object_finish_gpu(obj);
  1722. if (ret == -ERESTARTSYS)
  1723. return ret;
  1724. /* Continue on if we fail due to EIO, the GPU is hung so we
  1725. * should be safe and we need to cleanup or else we might
  1726. * cause memory corruption through use-after-free.
  1727. */
  1728. i915_gem_object_finish_gtt(obj);
  1729. /* Move the object to the CPU domain to ensure that
  1730. * any possible CPU writes while it's not in the GTT
  1731. * are flushed when we go to remap it.
  1732. */
  1733. if (ret == 0)
  1734. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1735. if (ret == -ERESTARTSYS)
  1736. return ret;
  1737. if (ret) {
  1738. /* In the event of a disaster, abandon all caches and
  1739. * hope for the best.
  1740. */
  1741. i915_gem_clflush_object(obj);
  1742. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1743. }
  1744. /* release the fence reg _after_ flushing */
  1745. ret = i915_gem_object_put_fence(obj);
  1746. if (ret == -ERESTARTSYS)
  1747. return ret;
  1748. trace_i915_gem_object_unbind(obj);
  1749. i915_gem_gtt_unbind_object(obj);
  1750. if (obj->has_aliasing_ppgtt_mapping) {
  1751. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  1752. obj->has_aliasing_ppgtt_mapping = 0;
  1753. }
  1754. i915_gem_object_put_pages_gtt(obj);
  1755. list_del_init(&obj->gtt_list);
  1756. list_del_init(&obj->mm_list);
  1757. /* Avoid an unnecessary call to unbind on rebind. */
  1758. obj->map_and_fenceable = true;
  1759. drm_mm_put_block(obj->gtt_space);
  1760. obj->gtt_space = NULL;
  1761. obj->gtt_offset = 0;
  1762. if (i915_gem_object_is_purgeable(obj))
  1763. i915_gem_object_truncate(obj);
  1764. return ret;
  1765. }
  1766. int
  1767. i915_gem_flush_ring(struct intel_ring_buffer *ring,
  1768. uint32_t invalidate_domains,
  1769. uint32_t flush_domains)
  1770. {
  1771. int ret;
  1772. if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
  1773. return 0;
  1774. trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
  1775. ret = ring->flush(ring, invalidate_domains, flush_domains);
  1776. if (ret)
  1777. return ret;
  1778. if (flush_domains & I915_GEM_GPU_DOMAINS)
  1779. i915_gem_process_flushing_list(ring, flush_domains);
  1780. return 0;
  1781. }
  1782. static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
  1783. {
  1784. int ret;
  1785. if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
  1786. return 0;
  1787. if (!list_empty(&ring->gpu_write_list)) {
  1788. ret = i915_gem_flush_ring(ring,
  1789. I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1790. if (ret)
  1791. return ret;
  1792. }
  1793. return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
  1794. do_retire);
  1795. }
  1796. int i915_gpu_idle(struct drm_device *dev, bool do_retire)
  1797. {
  1798. drm_i915_private_t *dev_priv = dev->dev_private;
  1799. int ret, i;
  1800. /* Flush everything onto the inactive list. */
  1801. for (i = 0; i < I915_NUM_RINGS; i++) {
  1802. ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
  1803. if (ret)
  1804. return ret;
  1805. }
  1806. return 0;
  1807. }
  1808. static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
  1809. struct intel_ring_buffer *pipelined)
  1810. {
  1811. struct drm_device *dev = obj->base.dev;
  1812. drm_i915_private_t *dev_priv = dev->dev_private;
  1813. u32 size = obj->gtt_space->size;
  1814. int regnum = obj->fence_reg;
  1815. uint64_t val;
  1816. /* Adjust fence size to match tiled area */
  1817. if (obj->tiling_mode != I915_TILING_NONE) {
  1818. uint32_t row_size = obj->stride *
  1819. (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
  1820. size = (size / row_size) * row_size;
  1821. }
  1822. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1823. 0xfffff000) << 32;
  1824. val |= obj->gtt_offset & 0xfffff000;
  1825. val |= (uint64_t)((obj->stride / 128) - 1) <<
  1826. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1827. if (obj->tiling_mode == I915_TILING_Y)
  1828. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1829. val |= I965_FENCE_REG_VALID;
  1830. if (pipelined) {
  1831. int ret = intel_ring_begin(pipelined, 6);
  1832. if (ret)
  1833. return ret;
  1834. intel_ring_emit(pipelined, MI_NOOP);
  1835. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
  1836. intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
  1837. intel_ring_emit(pipelined, (u32)val);
  1838. intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
  1839. intel_ring_emit(pipelined, (u32)(val >> 32));
  1840. intel_ring_advance(pipelined);
  1841. } else
  1842. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
  1843. return 0;
  1844. }
  1845. static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
  1846. struct intel_ring_buffer *pipelined)
  1847. {
  1848. struct drm_device *dev = obj->base.dev;
  1849. drm_i915_private_t *dev_priv = dev->dev_private;
  1850. u32 size = obj->gtt_space->size;
  1851. int regnum = obj->fence_reg;
  1852. uint64_t val;
  1853. /* Adjust fence size to match tiled area */
  1854. if (obj->tiling_mode != I915_TILING_NONE) {
  1855. uint32_t row_size = obj->stride *
  1856. (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
  1857. size = (size / row_size) * row_size;
  1858. }
  1859. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1860. 0xfffff000) << 32;
  1861. val |= obj->gtt_offset & 0xfffff000;
  1862. val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1863. if (obj->tiling_mode == I915_TILING_Y)
  1864. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1865. val |= I965_FENCE_REG_VALID;
  1866. if (pipelined) {
  1867. int ret = intel_ring_begin(pipelined, 6);
  1868. if (ret)
  1869. return ret;
  1870. intel_ring_emit(pipelined, MI_NOOP);
  1871. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
  1872. intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
  1873. intel_ring_emit(pipelined, (u32)val);
  1874. intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
  1875. intel_ring_emit(pipelined, (u32)(val >> 32));
  1876. intel_ring_advance(pipelined);
  1877. } else
  1878. I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
  1879. return 0;
  1880. }
  1881. static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
  1882. struct intel_ring_buffer *pipelined)
  1883. {
  1884. struct drm_device *dev = obj->base.dev;
  1885. drm_i915_private_t *dev_priv = dev->dev_private;
  1886. u32 size = obj->gtt_space->size;
  1887. u32 fence_reg, val, pitch_val;
  1888. int tile_width;
  1889. if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  1890. (size & -size) != size ||
  1891. (obj->gtt_offset & (size - 1)),
  1892. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  1893. obj->gtt_offset, obj->map_and_fenceable, size))
  1894. return -EINVAL;
  1895. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  1896. tile_width = 128;
  1897. else
  1898. tile_width = 512;
  1899. /* Note: pitch better be a power of two tile widths */
  1900. pitch_val = obj->stride / tile_width;
  1901. pitch_val = ffs(pitch_val) - 1;
  1902. val = obj->gtt_offset;
  1903. if (obj->tiling_mode == I915_TILING_Y)
  1904. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1905. val |= I915_FENCE_SIZE_BITS(size);
  1906. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1907. val |= I830_FENCE_REG_VALID;
  1908. fence_reg = obj->fence_reg;
  1909. if (fence_reg < 8)
  1910. fence_reg = FENCE_REG_830_0 + fence_reg * 4;
  1911. else
  1912. fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
  1913. if (pipelined) {
  1914. int ret = intel_ring_begin(pipelined, 4);
  1915. if (ret)
  1916. return ret;
  1917. intel_ring_emit(pipelined, MI_NOOP);
  1918. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
  1919. intel_ring_emit(pipelined, fence_reg);
  1920. intel_ring_emit(pipelined, val);
  1921. intel_ring_advance(pipelined);
  1922. } else
  1923. I915_WRITE(fence_reg, val);
  1924. return 0;
  1925. }
  1926. static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
  1927. struct intel_ring_buffer *pipelined)
  1928. {
  1929. struct drm_device *dev = obj->base.dev;
  1930. drm_i915_private_t *dev_priv = dev->dev_private;
  1931. u32 size = obj->gtt_space->size;
  1932. int regnum = obj->fence_reg;
  1933. uint32_t val;
  1934. uint32_t pitch_val;
  1935. if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  1936. (size & -size) != size ||
  1937. (obj->gtt_offset & (size - 1)),
  1938. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  1939. obj->gtt_offset, size))
  1940. return -EINVAL;
  1941. pitch_val = obj->stride / 128;
  1942. pitch_val = ffs(pitch_val) - 1;
  1943. val = obj->gtt_offset;
  1944. if (obj->tiling_mode == I915_TILING_Y)
  1945. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1946. val |= I830_FENCE_SIZE_BITS(size);
  1947. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1948. val |= I830_FENCE_REG_VALID;
  1949. if (pipelined) {
  1950. int ret = intel_ring_begin(pipelined, 4);
  1951. if (ret)
  1952. return ret;
  1953. intel_ring_emit(pipelined, MI_NOOP);
  1954. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
  1955. intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
  1956. intel_ring_emit(pipelined, val);
  1957. intel_ring_advance(pipelined);
  1958. } else
  1959. I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
  1960. return 0;
  1961. }
  1962. static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
  1963. {
  1964. return i915_seqno_passed(ring->get_seqno(ring), seqno);
  1965. }
  1966. static int
  1967. i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
  1968. struct intel_ring_buffer *pipelined)
  1969. {
  1970. int ret;
  1971. if (obj->fenced_gpu_access) {
  1972. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  1973. ret = i915_gem_flush_ring(obj->last_fenced_ring,
  1974. 0, obj->base.write_domain);
  1975. if (ret)
  1976. return ret;
  1977. }
  1978. obj->fenced_gpu_access = false;
  1979. }
  1980. if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
  1981. if (!ring_passed_seqno(obj->last_fenced_ring,
  1982. obj->last_fenced_seqno)) {
  1983. ret = i915_wait_request(obj->last_fenced_ring,
  1984. obj->last_fenced_seqno,
  1985. true);
  1986. if (ret)
  1987. return ret;
  1988. }
  1989. obj->last_fenced_seqno = 0;
  1990. obj->last_fenced_ring = NULL;
  1991. }
  1992. /* Ensure that all CPU reads are completed before installing a fence
  1993. * and all writes before removing the fence.
  1994. */
  1995. if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
  1996. mb();
  1997. return 0;
  1998. }
  1999. int
  2000. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2001. {
  2002. int ret;
  2003. if (obj->tiling_mode)
  2004. i915_gem_release_mmap(obj);
  2005. ret = i915_gem_object_flush_fence(obj, NULL);
  2006. if (ret)
  2007. return ret;
  2008. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2009. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2010. WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
  2011. i915_gem_clear_fence_reg(obj->base.dev,
  2012. &dev_priv->fence_regs[obj->fence_reg]);
  2013. obj->fence_reg = I915_FENCE_REG_NONE;
  2014. }
  2015. return 0;
  2016. }
  2017. static struct drm_i915_fence_reg *
  2018. i915_find_fence_reg(struct drm_device *dev,
  2019. struct intel_ring_buffer *pipelined)
  2020. {
  2021. struct drm_i915_private *dev_priv = dev->dev_private;
  2022. struct drm_i915_fence_reg *reg, *first, *avail;
  2023. int i;
  2024. /* First try to find a free reg */
  2025. avail = NULL;
  2026. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2027. reg = &dev_priv->fence_regs[i];
  2028. if (!reg->obj)
  2029. return reg;
  2030. if (!reg->pin_count)
  2031. avail = reg;
  2032. }
  2033. if (avail == NULL)
  2034. return NULL;
  2035. /* None available, try to steal one or wait for a user to finish */
  2036. avail = first = NULL;
  2037. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2038. if (reg->pin_count)
  2039. continue;
  2040. if (first == NULL)
  2041. first = reg;
  2042. if (!pipelined ||
  2043. !reg->obj->last_fenced_ring ||
  2044. reg->obj->last_fenced_ring == pipelined) {
  2045. avail = reg;
  2046. break;
  2047. }
  2048. }
  2049. if (avail == NULL)
  2050. avail = first;
  2051. return avail;
  2052. }
  2053. static void i915_gem_write_fence__ipi(void *data)
  2054. {
  2055. wbinvd();
  2056. }
  2057. /**
  2058. * i915_gem_object_get_fence - set up a fence reg for an object
  2059. * @obj: object to map through a fence reg
  2060. * @pipelined: ring on which to queue the change, or NULL for CPU access
  2061. * @interruptible: must we wait uninterruptibly for the register to retire?
  2062. *
  2063. * When mapping objects through the GTT, userspace wants to be able to write
  2064. * to them without having to worry about swizzling if the object is tiled.
  2065. *
  2066. * This function walks the fence regs looking for a free one for @obj,
  2067. * stealing one if it can't find any.
  2068. *
  2069. * It then sets up the reg based on the object's properties: address, pitch
  2070. * and tiling format.
  2071. */
  2072. int
  2073. i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
  2074. struct intel_ring_buffer *pipelined)
  2075. {
  2076. struct drm_device *dev = obj->base.dev;
  2077. struct drm_i915_private *dev_priv = dev->dev_private;
  2078. struct drm_i915_fence_reg *reg;
  2079. int ret;
  2080. /* XXX disable pipelining. There are bugs. Shocking. */
  2081. pipelined = NULL;
  2082. /* Just update our place in the LRU if our fence is getting reused. */
  2083. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2084. reg = &dev_priv->fence_regs[obj->fence_reg];
  2085. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2086. if (obj->tiling_changed) {
  2087. ret = i915_gem_object_flush_fence(obj, pipelined);
  2088. if (ret)
  2089. return ret;
  2090. if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
  2091. pipelined = NULL;
  2092. if (pipelined) {
  2093. reg->setup_seqno =
  2094. i915_gem_next_request_seqno(pipelined);
  2095. obj->last_fenced_seqno = reg->setup_seqno;
  2096. obj->last_fenced_ring = pipelined;
  2097. }
  2098. goto update;
  2099. }
  2100. if (!pipelined) {
  2101. if (reg->setup_seqno) {
  2102. if (!ring_passed_seqno(obj->last_fenced_ring,
  2103. reg->setup_seqno)) {
  2104. ret = i915_wait_request(obj->last_fenced_ring,
  2105. reg->setup_seqno,
  2106. true);
  2107. if (ret)
  2108. return ret;
  2109. }
  2110. reg->setup_seqno = 0;
  2111. }
  2112. } else if (obj->last_fenced_ring &&
  2113. obj->last_fenced_ring != pipelined) {
  2114. ret = i915_gem_object_flush_fence(obj, pipelined);
  2115. if (ret)
  2116. return ret;
  2117. }
  2118. return 0;
  2119. }
  2120. reg = i915_find_fence_reg(dev, pipelined);
  2121. if (reg == NULL)
  2122. return -EDEADLK;
  2123. ret = i915_gem_object_flush_fence(obj, pipelined);
  2124. if (ret)
  2125. return ret;
  2126. if (reg->obj) {
  2127. struct drm_i915_gem_object *old = reg->obj;
  2128. drm_gem_object_reference(&old->base);
  2129. if (old->tiling_mode)
  2130. i915_gem_release_mmap(old);
  2131. ret = i915_gem_object_flush_fence(old, pipelined);
  2132. if (ret) {
  2133. drm_gem_object_unreference(&old->base);
  2134. return ret;
  2135. }
  2136. if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
  2137. pipelined = NULL;
  2138. old->fence_reg = I915_FENCE_REG_NONE;
  2139. old->last_fenced_ring = pipelined;
  2140. old->last_fenced_seqno =
  2141. pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
  2142. drm_gem_object_unreference(&old->base);
  2143. } else if (obj->last_fenced_seqno == 0)
  2144. pipelined = NULL;
  2145. reg->obj = obj;
  2146. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2147. obj->fence_reg = reg - dev_priv->fence_regs;
  2148. obj->last_fenced_ring = pipelined;
  2149. reg->setup_seqno =
  2150. pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
  2151. obj->last_fenced_seqno = reg->setup_seqno;
  2152. update:
  2153. obj->tiling_changed = false;
  2154. switch (INTEL_INFO(dev)->gen) {
  2155. case 7:
  2156. case 6:
  2157. /* In order to fully serialize access to the fenced region and
  2158. * the update to the fence register we need to take extreme
  2159. * measures on SNB+. In theory, the write to the fence register
  2160. * flushes all memory transactions before, and coupled with the
  2161. * mb() placed around the register write we serialise all memory
  2162. * operations with respect to the changes in the tiler. Yet, on
  2163. * SNB+ we need to take a step further and emit an explicit wbinvd()
  2164. * on each processor in order to manually flush all memory
  2165. * transactions before updating the fence register.
  2166. */
  2167. on_each_cpu(i915_gem_write_fence__ipi, NULL, 1);
  2168. ret = sandybridge_write_fence_reg(obj, pipelined);
  2169. break;
  2170. case 5:
  2171. case 4:
  2172. ret = i965_write_fence_reg(obj, pipelined);
  2173. break;
  2174. case 3:
  2175. ret = i915_write_fence_reg(obj, pipelined);
  2176. break;
  2177. case 2:
  2178. ret = i830_write_fence_reg(obj, pipelined);
  2179. break;
  2180. }
  2181. return ret;
  2182. }
  2183. /**
  2184. * i915_gem_clear_fence_reg - clear out fence register info
  2185. * @obj: object to clear
  2186. *
  2187. * Zeroes out the fence register itself and clears out the associated
  2188. * data structures in dev_priv and obj.
  2189. */
  2190. static void
  2191. i915_gem_clear_fence_reg(struct drm_device *dev,
  2192. struct drm_i915_fence_reg *reg)
  2193. {
  2194. drm_i915_private_t *dev_priv = dev->dev_private;
  2195. uint32_t fence_reg = reg - dev_priv->fence_regs;
  2196. switch (INTEL_INFO(dev)->gen) {
  2197. case 7:
  2198. case 6:
  2199. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
  2200. break;
  2201. case 5:
  2202. case 4:
  2203. I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
  2204. break;
  2205. case 3:
  2206. if (fence_reg >= 8)
  2207. fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
  2208. else
  2209. case 2:
  2210. fence_reg = FENCE_REG_830_0 + fence_reg * 4;
  2211. I915_WRITE(fence_reg, 0);
  2212. break;
  2213. }
  2214. list_del_init(&reg->lru_list);
  2215. reg->obj = NULL;
  2216. reg->setup_seqno = 0;
  2217. reg->pin_count = 0;
  2218. }
  2219. /**
  2220. * Finds free space in the GTT aperture and binds the object there.
  2221. */
  2222. static int
  2223. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2224. unsigned alignment,
  2225. bool map_and_fenceable)
  2226. {
  2227. struct drm_device *dev = obj->base.dev;
  2228. drm_i915_private_t *dev_priv = dev->dev_private;
  2229. struct drm_mm_node *free_space;
  2230. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2231. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2232. bool mappable, fenceable;
  2233. int ret;
  2234. if (obj->madv != I915_MADV_WILLNEED) {
  2235. DRM_ERROR("Attempting to bind a purgeable object\n");
  2236. return -EINVAL;
  2237. }
  2238. fence_size = i915_gem_get_gtt_size(dev,
  2239. obj->base.size,
  2240. obj->tiling_mode);
  2241. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2242. obj->base.size,
  2243. obj->tiling_mode);
  2244. unfenced_alignment =
  2245. i915_gem_get_unfenced_gtt_alignment(dev,
  2246. obj->base.size,
  2247. obj->tiling_mode);
  2248. if (alignment == 0)
  2249. alignment = map_and_fenceable ? fence_alignment :
  2250. unfenced_alignment;
  2251. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2252. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2253. return -EINVAL;
  2254. }
  2255. size = map_and_fenceable ? fence_size : obj->base.size;
  2256. /* If the object is bigger than the entire aperture, reject it early
  2257. * before evicting everything in a vain attempt to find space.
  2258. */
  2259. if (obj->base.size >
  2260. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2261. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2262. return -E2BIG;
  2263. }
  2264. search_free:
  2265. if (map_and_fenceable)
  2266. free_space =
  2267. drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
  2268. size, alignment, 0,
  2269. dev_priv->mm.gtt_mappable_end,
  2270. 0);
  2271. else
  2272. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2273. size, alignment, 0);
  2274. if (free_space != NULL) {
  2275. if (map_and_fenceable)
  2276. obj->gtt_space =
  2277. drm_mm_get_block_range_generic(free_space,
  2278. size, alignment, 0,
  2279. dev_priv->mm.gtt_mappable_end,
  2280. 0);
  2281. else
  2282. obj->gtt_space =
  2283. drm_mm_get_block(free_space, size, alignment);
  2284. }
  2285. if (obj->gtt_space == NULL) {
  2286. /* If the gtt is empty and we're still having trouble
  2287. * fitting our object in, we're out of memory.
  2288. */
  2289. ret = i915_gem_evict_something(dev, size, alignment,
  2290. map_and_fenceable);
  2291. if (ret)
  2292. return ret;
  2293. goto search_free;
  2294. }
  2295. ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
  2296. if (ret) {
  2297. drm_mm_put_block(obj->gtt_space);
  2298. obj->gtt_space = NULL;
  2299. if (ret == -ENOMEM) {
  2300. /* first try to reclaim some memory by clearing the GTT */
  2301. ret = i915_gem_evict_everything(dev, false);
  2302. if (ret) {
  2303. /* now try to shrink everyone else */
  2304. if (gfpmask) {
  2305. gfpmask = 0;
  2306. goto search_free;
  2307. }
  2308. return -ENOMEM;
  2309. }
  2310. goto search_free;
  2311. }
  2312. return ret;
  2313. }
  2314. ret = i915_gem_gtt_bind_object(obj);
  2315. if (ret) {
  2316. i915_gem_object_put_pages_gtt(obj);
  2317. drm_mm_put_block(obj->gtt_space);
  2318. obj->gtt_space = NULL;
  2319. if (i915_gem_evict_everything(dev, false))
  2320. return ret;
  2321. goto search_free;
  2322. }
  2323. list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
  2324. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2325. /* Assert that the object is not currently in any GPU domain. As it
  2326. * wasn't in the GTT, there shouldn't be any way it could have been in
  2327. * a GPU cache
  2328. */
  2329. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2330. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2331. obj->gtt_offset = obj->gtt_space->start;
  2332. fenceable =
  2333. obj->gtt_space->size == fence_size &&
  2334. (obj->gtt_space->start & (fence_alignment - 1)) == 0;
  2335. mappable =
  2336. obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
  2337. obj->map_and_fenceable = mappable && fenceable;
  2338. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2339. return 0;
  2340. }
  2341. void
  2342. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2343. {
  2344. /* If we don't have a page list set up, then we're not pinned
  2345. * to GPU, and we can ignore the cache flush because it'll happen
  2346. * again at bind time.
  2347. */
  2348. if (obj->pages == NULL)
  2349. return;
  2350. /* If the GPU is snooping the contents of the CPU cache,
  2351. * we do not need to manually clear the CPU cache lines. However,
  2352. * the caches are only snooped when the render cache is
  2353. * flushed/invalidated. As we always have to emit invalidations
  2354. * and flushes when moving into and out of the RENDER domain, correct
  2355. * snooping behaviour occurs naturally as the result of our domain
  2356. * tracking.
  2357. */
  2358. if (obj->cache_level != I915_CACHE_NONE)
  2359. return;
  2360. trace_i915_gem_object_clflush(obj);
  2361. drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
  2362. }
  2363. /** Flushes any GPU write domain for the object if it's dirty. */
  2364. static int
  2365. i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
  2366. {
  2367. if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2368. return 0;
  2369. /* Queue the GPU write cache flushing we need. */
  2370. return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
  2371. }
  2372. /** Flushes the GTT write domain for the object if it's dirty. */
  2373. static void
  2374. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2375. {
  2376. uint32_t old_write_domain;
  2377. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2378. return;
  2379. /* No actual flushing is required for the GTT write domain. Writes
  2380. * to it immediately go to main memory as far as we know, so there's
  2381. * no chipset flush. It also doesn't land in render cache.
  2382. *
  2383. * However, we do have to enforce the order so that all writes through
  2384. * the GTT land before any writes to the device, such as updates to
  2385. * the GATT itself.
  2386. */
  2387. wmb();
  2388. old_write_domain = obj->base.write_domain;
  2389. obj->base.write_domain = 0;
  2390. trace_i915_gem_object_change_domain(obj,
  2391. obj->base.read_domains,
  2392. old_write_domain);
  2393. }
  2394. /** Flushes the CPU write domain for the object if it's dirty. */
  2395. static void
  2396. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2397. {
  2398. uint32_t old_write_domain;
  2399. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2400. return;
  2401. i915_gem_clflush_object(obj);
  2402. intel_gtt_chipset_flush();
  2403. old_write_domain = obj->base.write_domain;
  2404. obj->base.write_domain = 0;
  2405. trace_i915_gem_object_change_domain(obj,
  2406. obj->base.read_domains,
  2407. old_write_domain);
  2408. }
  2409. /**
  2410. * Moves a single object to the GTT read, and possibly write domain.
  2411. *
  2412. * This function returns when the move is complete, including waiting on
  2413. * flushes to occur.
  2414. */
  2415. int
  2416. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2417. {
  2418. uint32_t old_write_domain, old_read_domains;
  2419. int ret;
  2420. /* Not valid to be called on unbound objects. */
  2421. if (obj->gtt_space == NULL)
  2422. return -EINVAL;
  2423. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2424. return 0;
  2425. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2426. if (ret)
  2427. return ret;
  2428. if (obj->pending_gpu_write || write) {
  2429. ret = i915_gem_object_wait_rendering(obj);
  2430. if (ret)
  2431. return ret;
  2432. }
  2433. i915_gem_object_flush_cpu_write_domain(obj);
  2434. old_write_domain = obj->base.write_domain;
  2435. old_read_domains = obj->base.read_domains;
  2436. /* It should now be out of any other write domains, and we can update
  2437. * the domain values for our changes.
  2438. */
  2439. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2440. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2441. if (write) {
  2442. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2443. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2444. obj->dirty = 1;
  2445. }
  2446. trace_i915_gem_object_change_domain(obj,
  2447. old_read_domains,
  2448. old_write_domain);
  2449. return 0;
  2450. }
  2451. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2452. enum i915_cache_level cache_level)
  2453. {
  2454. struct drm_device *dev = obj->base.dev;
  2455. drm_i915_private_t *dev_priv = dev->dev_private;
  2456. int ret;
  2457. if (obj->cache_level == cache_level)
  2458. return 0;
  2459. if (obj->pin_count) {
  2460. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2461. return -EBUSY;
  2462. }
  2463. if (obj->gtt_space) {
  2464. ret = i915_gem_object_finish_gpu(obj);
  2465. if (ret)
  2466. return ret;
  2467. i915_gem_object_finish_gtt(obj);
  2468. /* Before SandyBridge, you could not use tiling or fence
  2469. * registers with snooped memory, so relinquish any fences
  2470. * currently pointing to our region in the aperture.
  2471. */
  2472. if (INTEL_INFO(obj->base.dev)->gen < 6) {
  2473. ret = i915_gem_object_put_fence(obj);
  2474. if (ret)
  2475. return ret;
  2476. }
  2477. i915_gem_gtt_rebind_object(obj, cache_level);
  2478. if (obj->has_aliasing_ppgtt_mapping)
  2479. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2480. obj, cache_level);
  2481. }
  2482. if (cache_level == I915_CACHE_NONE) {
  2483. u32 old_read_domains, old_write_domain;
  2484. /* If we're coming from LLC cached, then we haven't
  2485. * actually been tracking whether the data is in the
  2486. * CPU cache or not, since we only allow one bit set
  2487. * in obj->write_domain and have been skipping the clflushes.
  2488. * Just set it to the CPU cache for now.
  2489. */
  2490. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2491. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2492. old_read_domains = obj->base.read_domains;
  2493. old_write_domain = obj->base.write_domain;
  2494. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2495. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2496. trace_i915_gem_object_change_domain(obj,
  2497. old_read_domains,
  2498. old_write_domain);
  2499. }
  2500. obj->cache_level = cache_level;
  2501. return 0;
  2502. }
  2503. /*
  2504. * Prepare buffer for display plane (scanout, cursors, etc).
  2505. * Can be called from an uninterruptible phase (modesetting) and allows
  2506. * any flushes to be pipelined (for pageflips).
  2507. *
  2508. * For the display plane, we want to be in the GTT but out of any write
  2509. * domains. So in many ways this looks like set_to_gtt_domain() apart from the
  2510. * ability to pipeline the waits, pinning and any additional subtleties
  2511. * that may differentiate the display plane from ordinary buffers.
  2512. */
  2513. int
  2514. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2515. u32 alignment,
  2516. struct intel_ring_buffer *pipelined)
  2517. {
  2518. u32 old_read_domains, old_write_domain;
  2519. int ret;
  2520. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2521. if (ret)
  2522. return ret;
  2523. if (pipelined != obj->ring) {
  2524. ret = i915_gem_object_wait_rendering(obj);
  2525. if (ret == -ERESTARTSYS)
  2526. return ret;
  2527. }
  2528. /* The display engine is not coherent with the LLC cache on gen6. As
  2529. * a result, we make sure that the pinning that is about to occur is
  2530. * done with uncached PTEs. This is lowest common denominator for all
  2531. * chipsets.
  2532. *
  2533. * However for gen6+, we could do better by using the GFDT bit instead
  2534. * of uncaching, which would allow us to flush all the LLC-cached data
  2535. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2536. */
  2537. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2538. if (ret)
  2539. return ret;
  2540. /* As the user may map the buffer once pinned in the display plane
  2541. * (e.g. libkms for the bootup splash), we have to ensure that we
  2542. * always use map_and_fenceable for all scanout buffers.
  2543. */
  2544. ret = i915_gem_object_pin(obj, alignment, true);
  2545. if (ret)
  2546. return ret;
  2547. i915_gem_object_flush_cpu_write_domain(obj);
  2548. old_write_domain = obj->base.write_domain;
  2549. old_read_domains = obj->base.read_domains;
  2550. /* It should now be out of any other write domains, and we can update
  2551. * the domain values for our changes.
  2552. */
  2553. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2554. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2555. trace_i915_gem_object_change_domain(obj,
  2556. old_read_domains,
  2557. old_write_domain);
  2558. return 0;
  2559. }
  2560. int
  2561. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2562. {
  2563. int ret;
  2564. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2565. return 0;
  2566. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2567. ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
  2568. if (ret)
  2569. return ret;
  2570. }
  2571. ret = i915_gem_object_wait_rendering(obj);
  2572. if (ret)
  2573. return ret;
  2574. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2575. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2576. return 0;
  2577. }
  2578. /**
  2579. * Moves a single object to the CPU read, and possibly write domain.
  2580. *
  2581. * This function returns when the move is complete, including waiting on
  2582. * flushes to occur.
  2583. */
  2584. static int
  2585. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2586. {
  2587. uint32_t old_write_domain, old_read_domains;
  2588. int ret;
  2589. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2590. return 0;
  2591. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2592. if (ret)
  2593. return ret;
  2594. ret = i915_gem_object_wait_rendering(obj);
  2595. if (ret)
  2596. return ret;
  2597. i915_gem_object_flush_gtt_write_domain(obj);
  2598. /* If we have a partially-valid cache of the object in the CPU,
  2599. * finish invalidating it and free the per-page flags.
  2600. */
  2601. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2602. old_write_domain = obj->base.write_domain;
  2603. old_read_domains = obj->base.read_domains;
  2604. /* Flush the CPU cache if it's still invalid. */
  2605. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2606. i915_gem_clflush_object(obj);
  2607. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2608. }
  2609. /* It should now be out of any other write domains, and we can update
  2610. * the domain values for our changes.
  2611. */
  2612. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2613. /* If we're writing through the CPU, then the GPU read domains will
  2614. * need to be invalidated at next use.
  2615. */
  2616. if (write) {
  2617. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2618. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2619. }
  2620. trace_i915_gem_object_change_domain(obj,
  2621. old_read_domains,
  2622. old_write_domain);
  2623. return 0;
  2624. }
  2625. /**
  2626. * Moves the object from a partially CPU read to a full one.
  2627. *
  2628. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2629. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2630. */
  2631. static void
  2632. i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
  2633. {
  2634. if (!obj->page_cpu_valid)
  2635. return;
  2636. /* If we're partially in the CPU read domain, finish moving it in.
  2637. */
  2638. if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
  2639. int i;
  2640. for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
  2641. if (obj->page_cpu_valid[i])
  2642. continue;
  2643. drm_clflush_pages(obj->pages + i, 1);
  2644. }
  2645. }
  2646. /* Free the page_cpu_valid mappings which are now stale, whether
  2647. * or not we've got I915_GEM_DOMAIN_CPU.
  2648. */
  2649. kfree(obj->page_cpu_valid);
  2650. obj->page_cpu_valid = NULL;
  2651. }
  2652. /**
  2653. * Set the CPU read domain on a range of the object.
  2654. *
  2655. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2656. * not entirely valid. The page_cpu_valid member of the object flags which
  2657. * pages have been flushed, and will be respected by
  2658. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2659. * of the whole object.
  2660. *
  2661. * This function returns when the move is complete, including waiting on
  2662. * flushes to occur.
  2663. */
  2664. static int
  2665. i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
  2666. uint64_t offset, uint64_t size)
  2667. {
  2668. uint32_t old_read_domains;
  2669. int i, ret;
  2670. if (offset == 0 && size == obj->base.size)
  2671. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2672. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2673. if (ret)
  2674. return ret;
  2675. ret = i915_gem_object_wait_rendering(obj);
  2676. if (ret)
  2677. return ret;
  2678. i915_gem_object_flush_gtt_write_domain(obj);
  2679. /* If we're already fully in the CPU read domain, we're done. */
  2680. if (obj->page_cpu_valid == NULL &&
  2681. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2682. return 0;
  2683. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2684. * newly adding I915_GEM_DOMAIN_CPU
  2685. */
  2686. if (obj->page_cpu_valid == NULL) {
  2687. obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
  2688. GFP_KERNEL);
  2689. if (obj->page_cpu_valid == NULL)
  2690. return -ENOMEM;
  2691. } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2692. memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
  2693. /* Flush the cache on any pages that are still invalid from the CPU's
  2694. * perspective.
  2695. */
  2696. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2697. i++) {
  2698. if (obj->page_cpu_valid[i])
  2699. continue;
  2700. drm_clflush_pages(obj->pages + i, 1);
  2701. obj->page_cpu_valid[i] = 1;
  2702. }
  2703. /* It should now be out of any other write domains, and we can update
  2704. * the domain values for our changes.
  2705. */
  2706. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2707. old_read_domains = obj->base.read_domains;
  2708. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2709. trace_i915_gem_object_change_domain(obj,
  2710. old_read_domains,
  2711. obj->base.write_domain);
  2712. return 0;
  2713. }
  2714. /* Throttle our rendering by waiting until the ring has completed our requests
  2715. * emitted over 20 msec ago.
  2716. *
  2717. * Note that if we were to use the current jiffies each time around the loop,
  2718. * we wouldn't escape the function with any frames outstanding if the time to
  2719. * render a frame was over 20ms.
  2720. *
  2721. * This should get us reasonable parallelism between CPU and GPU but also
  2722. * relatively low latency when blocking on a particular request to finish.
  2723. */
  2724. static int
  2725. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2726. {
  2727. struct drm_i915_private *dev_priv = dev->dev_private;
  2728. struct drm_i915_file_private *file_priv = file->driver_priv;
  2729. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2730. struct drm_i915_gem_request *request;
  2731. struct intel_ring_buffer *ring = NULL;
  2732. u32 seqno = 0;
  2733. int ret;
  2734. if (atomic_read(&dev_priv->mm.wedged))
  2735. return -EIO;
  2736. spin_lock(&file_priv->mm.lock);
  2737. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2738. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2739. break;
  2740. ring = request->ring;
  2741. seqno = request->seqno;
  2742. }
  2743. spin_unlock(&file_priv->mm.lock);
  2744. if (seqno == 0)
  2745. return 0;
  2746. ret = 0;
  2747. if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
  2748. /* And wait for the seqno passing without holding any locks and
  2749. * causing extra latency for others. This is safe as the irq
  2750. * generation is designed to be run atomically and so is
  2751. * lockless.
  2752. */
  2753. if (ring->irq_get(ring)) {
  2754. ret = wait_event_interruptible(ring->irq_queue,
  2755. i915_seqno_passed(ring->get_seqno(ring), seqno)
  2756. || atomic_read(&dev_priv->mm.wedged));
  2757. ring->irq_put(ring);
  2758. if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
  2759. ret = -EIO;
  2760. } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
  2761. seqno) ||
  2762. atomic_read(&dev_priv->mm.wedged), 3000)) {
  2763. ret = -EBUSY;
  2764. }
  2765. }
  2766. if (ret == 0)
  2767. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2768. return ret;
  2769. }
  2770. int
  2771. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2772. uint32_t alignment,
  2773. bool map_and_fenceable)
  2774. {
  2775. struct drm_device *dev = obj->base.dev;
  2776. struct drm_i915_private *dev_priv = dev->dev_private;
  2777. int ret;
  2778. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  2779. return -EBUSY;
  2780. WARN_ON(i915_verify_lists(dev));
  2781. if (obj->gtt_space != NULL) {
  2782. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2783. (map_and_fenceable && !obj->map_and_fenceable)) {
  2784. WARN(obj->pin_count,
  2785. "bo is already pinned with incorrect alignment:"
  2786. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2787. " obj->map_and_fenceable=%d\n",
  2788. obj->gtt_offset, alignment,
  2789. map_and_fenceable,
  2790. obj->map_and_fenceable);
  2791. ret = i915_gem_object_unbind(obj);
  2792. if (ret)
  2793. return ret;
  2794. }
  2795. }
  2796. if (obj->gtt_space == NULL) {
  2797. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2798. map_and_fenceable);
  2799. if (ret)
  2800. return ret;
  2801. }
  2802. if (obj->pin_count++ == 0) {
  2803. if (!obj->active)
  2804. list_move_tail(&obj->mm_list,
  2805. &dev_priv->mm.pinned_list);
  2806. }
  2807. obj->pin_mappable |= map_and_fenceable;
  2808. WARN_ON(i915_verify_lists(dev));
  2809. return 0;
  2810. }
  2811. void
  2812. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2813. {
  2814. struct drm_device *dev = obj->base.dev;
  2815. drm_i915_private_t *dev_priv = dev->dev_private;
  2816. WARN_ON(i915_verify_lists(dev));
  2817. BUG_ON(obj->pin_count == 0);
  2818. BUG_ON(obj->gtt_space == NULL);
  2819. if (--obj->pin_count == 0) {
  2820. if (!obj->active)
  2821. list_move_tail(&obj->mm_list,
  2822. &dev_priv->mm.inactive_list);
  2823. obj->pin_mappable = false;
  2824. }
  2825. WARN_ON(i915_verify_lists(dev));
  2826. }
  2827. int
  2828. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2829. struct drm_file *file)
  2830. {
  2831. struct drm_i915_gem_pin *args = data;
  2832. struct drm_i915_gem_object *obj;
  2833. int ret;
  2834. ret = i915_mutex_lock_interruptible(dev);
  2835. if (ret)
  2836. return ret;
  2837. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2838. if (&obj->base == NULL) {
  2839. ret = -ENOENT;
  2840. goto unlock;
  2841. }
  2842. if (obj->madv != I915_MADV_WILLNEED) {
  2843. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2844. ret = -EINVAL;
  2845. goto out;
  2846. }
  2847. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2848. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2849. args->handle);
  2850. ret = -EINVAL;
  2851. goto out;
  2852. }
  2853. if (obj->user_pin_count == 0) {
  2854. ret = i915_gem_object_pin(obj, args->alignment, true);
  2855. if (ret)
  2856. goto out;
  2857. }
  2858. obj->user_pin_count++;
  2859. obj->pin_filp = file;
  2860. /* XXX - flush the CPU caches for pinned objects
  2861. * as the X server doesn't manage domains yet
  2862. */
  2863. i915_gem_object_flush_cpu_write_domain(obj);
  2864. args->offset = obj->gtt_offset;
  2865. out:
  2866. drm_gem_object_unreference(&obj->base);
  2867. unlock:
  2868. mutex_unlock(&dev->struct_mutex);
  2869. return ret;
  2870. }
  2871. int
  2872. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2873. struct drm_file *file)
  2874. {
  2875. struct drm_i915_gem_pin *args = data;
  2876. struct drm_i915_gem_object *obj;
  2877. int ret;
  2878. ret = i915_mutex_lock_interruptible(dev);
  2879. if (ret)
  2880. return ret;
  2881. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2882. if (&obj->base == NULL) {
  2883. ret = -ENOENT;
  2884. goto unlock;
  2885. }
  2886. if (obj->pin_filp != file) {
  2887. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2888. args->handle);
  2889. ret = -EINVAL;
  2890. goto out;
  2891. }
  2892. obj->user_pin_count--;
  2893. if (obj->user_pin_count == 0) {
  2894. obj->pin_filp = NULL;
  2895. i915_gem_object_unpin(obj);
  2896. }
  2897. out:
  2898. drm_gem_object_unreference(&obj->base);
  2899. unlock:
  2900. mutex_unlock(&dev->struct_mutex);
  2901. return ret;
  2902. }
  2903. int
  2904. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2905. struct drm_file *file)
  2906. {
  2907. struct drm_i915_gem_busy *args = data;
  2908. struct drm_i915_gem_object *obj;
  2909. int ret;
  2910. ret = i915_mutex_lock_interruptible(dev);
  2911. if (ret)
  2912. return ret;
  2913. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2914. if (&obj->base == NULL) {
  2915. ret = -ENOENT;
  2916. goto unlock;
  2917. }
  2918. /* Count all active objects as busy, even if they are currently not used
  2919. * by the gpu. Users of this interface expect objects to eventually
  2920. * become non-busy without any further actions, therefore emit any
  2921. * necessary flushes here.
  2922. */
  2923. args->busy = obj->active;
  2924. if (args->busy) {
  2925. /* Unconditionally flush objects, even when the gpu still uses this
  2926. * object. Userspace calling this function indicates that it wants to
  2927. * use this buffer rather sooner than later, so issuing the required
  2928. * flush earlier is beneficial.
  2929. */
  2930. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2931. ret = i915_gem_flush_ring(obj->ring,
  2932. 0, obj->base.write_domain);
  2933. } else if (obj->ring->outstanding_lazy_request ==
  2934. obj->last_rendering_seqno) {
  2935. struct drm_i915_gem_request *request;
  2936. /* This ring is not being cleared by active usage,
  2937. * so emit a request to do so.
  2938. */
  2939. request = kzalloc(sizeof(*request), GFP_KERNEL);
  2940. if (request) {
  2941. ret = i915_add_request(obj->ring, NULL, request);
  2942. if (ret)
  2943. kfree(request);
  2944. } else
  2945. ret = -ENOMEM;
  2946. }
  2947. /* Update the active list for the hardware's current position.
  2948. * Otherwise this only updates on a delayed timer or when irqs
  2949. * are actually unmasked, and our working set ends up being
  2950. * larger than required.
  2951. */
  2952. i915_gem_retire_requests_ring(obj->ring);
  2953. args->busy = obj->active;
  2954. }
  2955. drm_gem_object_unreference(&obj->base);
  2956. unlock:
  2957. mutex_unlock(&dev->struct_mutex);
  2958. return ret;
  2959. }
  2960. int
  2961. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2962. struct drm_file *file_priv)
  2963. {
  2964. return i915_gem_ring_throttle(dev, file_priv);
  2965. }
  2966. int
  2967. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  2968. struct drm_file *file_priv)
  2969. {
  2970. struct drm_i915_gem_madvise *args = data;
  2971. struct drm_i915_gem_object *obj;
  2972. int ret;
  2973. switch (args->madv) {
  2974. case I915_MADV_DONTNEED:
  2975. case I915_MADV_WILLNEED:
  2976. break;
  2977. default:
  2978. return -EINVAL;
  2979. }
  2980. ret = i915_mutex_lock_interruptible(dev);
  2981. if (ret)
  2982. return ret;
  2983. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  2984. if (&obj->base == NULL) {
  2985. ret = -ENOENT;
  2986. goto unlock;
  2987. }
  2988. if (obj->pin_count) {
  2989. ret = -EINVAL;
  2990. goto out;
  2991. }
  2992. if (obj->madv != __I915_MADV_PURGED)
  2993. obj->madv = args->madv;
  2994. /* if the object is no longer bound, discard its backing storage */
  2995. if (i915_gem_object_is_purgeable(obj) &&
  2996. obj->gtt_space == NULL)
  2997. i915_gem_object_truncate(obj);
  2998. args->retained = obj->madv != __I915_MADV_PURGED;
  2999. out:
  3000. drm_gem_object_unreference(&obj->base);
  3001. unlock:
  3002. mutex_unlock(&dev->struct_mutex);
  3003. return ret;
  3004. }
  3005. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3006. size_t size)
  3007. {
  3008. struct drm_i915_private *dev_priv = dev->dev_private;
  3009. struct drm_i915_gem_object *obj;
  3010. struct address_space *mapping;
  3011. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3012. if (obj == NULL)
  3013. return NULL;
  3014. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3015. kfree(obj);
  3016. return NULL;
  3017. }
  3018. mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3019. mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
  3020. i915_gem_info_add_obj(dev_priv, size);
  3021. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3022. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3023. if (HAS_LLC(dev)) {
  3024. /* On some devices, we can have the GPU use the LLC (the CPU
  3025. * cache) for about a 10% performance improvement
  3026. * compared to uncached. Graphics requests other than
  3027. * display scanout are coherent with the CPU in
  3028. * accessing this cache. This means in this mode we
  3029. * don't need to clflush on the CPU side, and on the
  3030. * GPU side we only need to flush internal caches to
  3031. * get data visible to the CPU.
  3032. *
  3033. * However, we maintain the display planes as UC, and so
  3034. * need to rebind when first used as such.
  3035. */
  3036. obj->cache_level = I915_CACHE_LLC;
  3037. } else
  3038. obj->cache_level = I915_CACHE_NONE;
  3039. obj->base.driver_private = NULL;
  3040. obj->fence_reg = I915_FENCE_REG_NONE;
  3041. INIT_LIST_HEAD(&obj->mm_list);
  3042. INIT_LIST_HEAD(&obj->gtt_list);
  3043. INIT_LIST_HEAD(&obj->ring_list);
  3044. INIT_LIST_HEAD(&obj->exec_list);
  3045. INIT_LIST_HEAD(&obj->gpu_write_list);
  3046. obj->madv = I915_MADV_WILLNEED;
  3047. /* Avoid an unnecessary call to unbind on the first bind. */
  3048. obj->map_and_fenceable = true;
  3049. return obj;
  3050. }
  3051. int i915_gem_init_object(struct drm_gem_object *obj)
  3052. {
  3053. BUG();
  3054. return 0;
  3055. }
  3056. static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
  3057. {
  3058. struct drm_device *dev = obj->base.dev;
  3059. drm_i915_private_t *dev_priv = dev->dev_private;
  3060. int ret;
  3061. ret = i915_gem_object_unbind(obj);
  3062. if (ret == -ERESTARTSYS) {
  3063. list_move(&obj->mm_list,
  3064. &dev_priv->mm.deferred_free_list);
  3065. return;
  3066. }
  3067. trace_i915_gem_object_destroy(obj);
  3068. if (obj->base.map_list.map)
  3069. drm_gem_free_mmap_offset(&obj->base);
  3070. drm_gem_object_release(&obj->base);
  3071. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3072. kfree(obj->page_cpu_valid);
  3073. kfree(obj->bit_17);
  3074. kfree(obj);
  3075. }
  3076. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3077. {
  3078. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3079. struct drm_device *dev = obj->base.dev;
  3080. while (obj->pin_count > 0)
  3081. i915_gem_object_unpin(obj);
  3082. if (obj->phys_obj)
  3083. i915_gem_detach_phys_object(dev, obj);
  3084. i915_gem_free_object_tail(obj);
  3085. }
  3086. int
  3087. i915_gem_idle(struct drm_device *dev)
  3088. {
  3089. drm_i915_private_t *dev_priv = dev->dev_private;
  3090. int ret;
  3091. mutex_lock(&dev->struct_mutex);
  3092. if (dev_priv->mm.suspended) {
  3093. mutex_unlock(&dev->struct_mutex);
  3094. return 0;
  3095. }
  3096. ret = i915_gpu_idle(dev, true);
  3097. if (ret) {
  3098. mutex_unlock(&dev->struct_mutex);
  3099. return ret;
  3100. }
  3101. /* Under UMS, be paranoid and evict. */
  3102. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3103. ret = i915_gem_evict_inactive(dev, false);
  3104. if (ret) {
  3105. mutex_unlock(&dev->struct_mutex);
  3106. return ret;
  3107. }
  3108. }
  3109. i915_gem_reset_fences(dev);
  3110. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3111. * We need to replace this with a semaphore, or something.
  3112. * And not confound mm.suspended!
  3113. */
  3114. dev_priv->mm.suspended = 1;
  3115. del_timer_sync(&dev_priv->hangcheck_timer);
  3116. i915_kernel_lost_context(dev);
  3117. i915_gem_cleanup_ringbuffer(dev);
  3118. mutex_unlock(&dev->struct_mutex);
  3119. /* Cancel the retire work handler, which should be idle now. */
  3120. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3121. return 0;
  3122. }
  3123. void i915_gem_init_swizzling(struct drm_device *dev)
  3124. {
  3125. drm_i915_private_t *dev_priv = dev->dev_private;
  3126. if (INTEL_INFO(dev)->gen < 5 ||
  3127. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3128. return;
  3129. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3130. DISP_TILE_SURFACE_SWIZZLING);
  3131. if (IS_GEN5(dev))
  3132. return;
  3133. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3134. if (IS_GEN6(dev))
  3135. I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3136. else
  3137. I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3138. }
  3139. void i915_gem_init_ppgtt(struct drm_device *dev)
  3140. {
  3141. drm_i915_private_t *dev_priv = dev->dev_private;
  3142. uint32_t pd_offset;
  3143. struct intel_ring_buffer *ring;
  3144. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  3145. uint32_t __iomem *pd_addr;
  3146. uint32_t pd_entry;
  3147. int i;
  3148. if (!dev_priv->mm.aliasing_ppgtt)
  3149. return;
  3150. pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
  3151. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  3152. dma_addr_t pt_addr;
  3153. if (dev_priv->mm.gtt->needs_dmar)
  3154. pt_addr = ppgtt->pt_dma_addr[i];
  3155. else
  3156. pt_addr = page_to_phys(ppgtt->pt_pages[i]);
  3157. pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
  3158. pd_entry |= GEN6_PDE_VALID;
  3159. writel(pd_entry, pd_addr + i);
  3160. }
  3161. readl(pd_addr);
  3162. pd_offset = ppgtt->pd_offset;
  3163. pd_offset /= 64; /* in cachelines, */
  3164. pd_offset <<= 16;
  3165. if (INTEL_INFO(dev)->gen == 6) {
  3166. uint32_t ecochk = I915_READ(GAM_ECOCHK);
  3167. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
  3168. ECOCHK_PPGTT_CACHE64B);
  3169. I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
  3170. } else if (INTEL_INFO(dev)->gen >= 7) {
  3171. I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
  3172. /* GFX_MODE is per-ring on gen7+ */
  3173. }
  3174. for (i = 0; i < I915_NUM_RINGS; i++) {
  3175. ring = &dev_priv->ring[i];
  3176. if (INTEL_INFO(dev)->gen >= 7)
  3177. I915_WRITE(RING_MODE_GEN7(ring),
  3178. GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
  3179. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  3180. I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
  3181. }
  3182. }
  3183. int
  3184. i915_gem_init_hw(struct drm_device *dev)
  3185. {
  3186. drm_i915_private_t *dev_priv = dev->dev_private;
  3187. int ret;
  3188. i915_gem_init_swizzling(dev);
  3189. ret = intel_init_render_ring_buffer(dev);
  3190. if (ret)
  3191. return ret;
  3192. if (HAS_BSD(dev)) {
  3193. ret = intel_init_bsd_ring_buffer(dev);
  3194. if (ret)
  3195. goto cleanup_render_ring;
  3196. }
  3197. if (HAS_BLT(dev)) {
  3198. ret = intel_init_blt_ring_buffer(dev);
  3199. if (ret)
  3200. goto cleanup_bsd_ring;
  3201. }
  3202. dev_priv->next_seqno = 1;
  3203. i915_gem_init_ppgtt(dev);
  3204. return 0;
  3205. cleanup_bsd_ring:
  3206. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3207. cleanup_render_ring:
  3208. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3209. return ret;
  3210. }
  3211. void
  3212. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3213. {
  3214. drm_i915_private_t *dev_priv = dev->dev_private;
  3215. int i;
  3216. for (i = 0; i < I915_NUM_RINGS; i++)
  3217. intel_cleanup_ring_buffer(&dev_priv->ring[i]);
  3218. }
  3219. int
  3220. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3221. struct drm_file *file_priv)
  3222. {
  3223. drm_i915_private_t *dev_priv = dev->dev_private;
  3224. int ret, i;
  3225. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3226. return 0;
  3227. if (atomic_read(&dev_priv->mm.wedged)) {
  3228. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3229. atomic_set(&dev_priv->mm.wedged, 0);
  3230. }
  3231. mutex_lock(&dev->struct_mutex);
  3232. dev_priv->mm.suspended = 0;
  3233. ret = i915_gem_init_hw(dev);
  3234. if (ret != 0) {
  3235. mutex_unlock(&dev->struct_mutex);
  3236. return ret;
  3237. }
  3238. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3239. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3240. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3241. for (i = 0; i < I915_NUM_RINGS; i++) {
  3242. BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
  3243. BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
  3244. }
  3245. mutex_unlock(&dev->struct_mutex);
  3246. ret = drm_irq_install(dev);
  3247. if (ret)
  3248. goto cleanup_ringbuffer;
  3249. return 0;
  3250. cleanup_ringbuffer:
  3251. mutex_lock(&dev->struct_mutex);
  3252. i915_gem_cleanup_ringbuffer(dev);
  3253. dev_priv->mm.suspended = 1;
  3254. mutex_unlock(&dev->struct_mutex);
  3255. return ret;
  3256. }
  3257. int
  3258. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3259. struct drm_file *file_priv)
  3260. {
  3261. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3262. return 0;
  3263. drm_irq_uninstall(dev);
  3264. return i915_gem_idle(dev);
  3265. }
  3266. void
  3267. i915_gem_lastclose(struct drm_device *dev)
  3268. {
  3269. int ret;
  3270. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3271. return;
  3272. ret = i915_gem_idle(dev);
  3273. if (ret)
  3274. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3275. }
  3276. static void
  3277. init_ring_lists(struct intel_ring_buffer *ring)
  3278. {
  3279. INIT_LIST_HEAD(&ring->active_list);
  3280. INIT_LIST_HEAD(&ring->request_list);
  3281. INIT_LIST_HEAD(&ring->gpu_write_list);
  3282. }
  3283. void
  3284. i915_gem_load(struct drm_device *dev)
  3285. {
  3286. int i;
  3287. drm_i915_private_t *dev_priv = dev->dev_private;
  3288. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3289. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  3290. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3291. INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
  3292. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3293. INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
  3294. INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
  3295. for (i = 0; i < I915_NUM_RINGS; i++)
  3296. init_ring_lists(&dev_priv->ring[i]);
  3297. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3298. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3299. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3300. i915_gem_retire_work_handler);
  3301. init_completion(&dev_priv->error_completion);
  3302. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3303. if (IS_GEN3(dev)) {
  3304. u32 tmp = I915_READ(MI_ARB_STATE);
  3305. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  3306. /* arb state is a masked write, so set bit + bit in mask */
  3307. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  3308. I915_WRITE(MI_ARB_STATE, tmp);
  3309. }
  3310. }
  3311. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3312. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3313. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3314. dev_priv->fence_reg_start = 3;
  3315. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3316. dev_priv->num_fence_regs = 16;
  3317. else
  3318. dev_priv->num_fence_regs = 8;
  3319. /* Initialize fence registers to zero */
  3320. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  3321. i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
  3322. }
  3323. i915_gem_detect_bit_6_swizzle(dev);
  3324. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3325. dev_priv->mm.interruptible = true;
  3326. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3327. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3328. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3329. }
  3330. /*
  3331. * Create a physically contiguous memory object for this object
  3332. * e.g. for cursor + overlay regs
  3333. */
  3334. static int i915_gem_init_phys_object(struct drm_device *dev,
  3335. int id, int size, int align)
  3336. {
  3337. drm_i915_private_t *dev_priv = dev->dev_private;
  3338. struct drm_i915_gem_phys_object *phys_obj;
  3339. int ret;
  3340. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3341. return 0;
  3342. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3343. if (!phys_obj)
  3344. return -ENOMEM;
  3345. phys_obj->id = id;
  3346. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3347. if (!phys_obj->handle) {
  3348. ret = -ENOMEM;
  3349. goto kfree_obj;
  3350. }
  3351. #ifdef CONFIG_X86
  3352. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3353. #endif
  3354. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3355. return 0;
  3356. kfree_obj:
  3357. kfree(phys_obj);
  3358. return ret;
  3359. }
  3360. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3361. {
  3362. drm_i915_private_t *dev_priv = dev->dev_private;
  3363. struct drm_i915_gem_phys_object *phys_obj;
  3364. if (!dev_priv->mm.phys_objs[id - 1])
  3365. return;
  3366. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3367. if (phys_obj->cur_obj) {
  3368. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3369. }
  3370. #ifdef CONFIG_X86
  3371. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3372. #endif
  3373. drm_pci_free(dev, phys_obj->handle);
  3374. kfree(phys_obj);
  3375. dev_priv->mm.phys_objs[id - 1] = NULL;
  3376. }
  3377. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3378. {
  3379. int i;
  3380. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3381. i915_gem_free_phys_object(dev, i);
  3382. }
  3383. void i915_gem_detach_phys_object(struct drm_device *dev,
  3384. struct drm_i915_gem_object *obj)
  3385. {
  3386. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3387. char *vaddr;
  3388. int i;
  3389. int page_count;
  3390. if (!obj->phys_obj)
  3391. return;
  3392. vaddr = obj->phys_obj->handle->vaddr;
  3393. page_count = obj->base.size / PAGE_SIZE;
  3394. for (i = 0; i < page_count; i++) {
  3395. struct page *page = shmem_read_mapping_page(mapping, i);
  3396. if (!IS_ERR(page)) {
  3397. char *dst = kmap_atomic(page);
  3398. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3399. kunmap_atomic(dst);
  3400. drm_clflush_pages(&page, 1);
  3401. set_page_dirty(page);
  3402. mark_page_accessed(page);
  3403. page_cache_release(page);
  3404. }
  3405. }
  3406. intel_gtt_chipset_flush();
  3407. obj->phys_obj->cur_obj = NULL;
  3408. obj->phys_obj = NULL;
  3409. }
  3410. int
  3411. i915_gem_attach_phys_object(struct drm_device *dev,
  3412. struct drm_i915_gem_object *obj,
  3413. int id,
  3414. int align)
  3415. {
  3416. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3417. drm_i915_private_t *dev_priv = dev->dev_private;
  3418. int ret = 0;
  3419. int page_count;
  3420. int i;
  3421. if (id > I915_MAX_PHYS_OBJECT)
  3422. return -EINVAL;
  3423. if (obj->phys_obj) {
  3424. if (obj->phys_obj->id == id)
  3425. return 0;
  3426. i915_gem_detach_phys_object(dev, obj);
  3427. }
  3428. /* create a new object */
  3429. if (!dev_priv->mm.phys_objs[id - 1]) {
  3430. ret = i915_gem_init_phys_object(dev, id,
  3431. obj->base.size, align);
  3432. if (ret) {
  3433. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3434. id, obj->base.size);
  3435. return ret;
  3436. }
  3437. }
  3438. /* bind to the object */
  3439. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3440. obj->phys_obj->cur_obj = obj;
  3441. page_count = obj->base.size / PAGE_SIZE;
  3442. for (i = 0; i < page_count; i++) {
  3443. struct page *page;
  3444. char *dst, *src;
  3445. page = shmem_read_mapping_page(mapping, i);
  3446. if (IS_ERR(page))
  3447. return PTR_ERR(page);
  3448. src = kmap_atomic(page);
  3449. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3450. memcpy(dst, src, PAGE_SIZE);
  3451. kunmap_atomic(src);
  3452. mark_page_accessed(page);
  3453. page_cache_release(page);
  3454. }
  3455. return 0;
  3456. }
  3457. static int
  3458. i915_gem_phys_pwrite(struct drm_device *dev,
  3459. struct drm_i915_gem_object *obj,
  3460. struct drm_i915_gem_pwrite *args,
  3461. struct drm_file *file_priv)
  3462. {
  3463. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3464. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  3465. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3466. unsigned long unwritten;
  3467. /* The physical object once assigned is fixed for the lifetime
  3468. * of the obj, so we can safely drop the lock and continue
  3469. * to access vaddr.
  3470. */
  3471. mutex_unlock(&dev->struct_mutex);
  3472. unwritten = copy_from_user(vaddr, user_data, args->size);
  3473. mutex_lock(&dev->struct_mutex);
  3474. if (unwritten)
  3475. return -EFAULT;
  3476. }
  3477. intel_gtt_chipset_flush();
  3478. return 0;
  3479. }
  3480. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3481. {
  3482. struct drm_i915_file_private *file_priv = file->driver_priv;
  3483. /* Clean up our request list when the client is going away, so that
  3484. * later retire_requests won't dereference our soon-to-be-gone
  3485. * file_priv.
  3486. */
  3487. spin_lock(&file_priv->mm.lock);
  3488. while (!list_empty(&file_priv->mm.request_list)) {
  3489. struct drm_i915_gem_request *request;
  3490. request = list_first_entry(&file_priv->mm.request_list,
  3491. struct drm_i915_gem_request,
  3492. client_list);
  3493. list_del(&request->client_list);
  3494. request->file_priv = NULL;
  3495. }
  3496. spin_unlock(&file_priv->mm.lock);
  3497. }
  3498. static int
  3499. i915_gpu_is_active(struct drm_device *dev)
  3500. {
  3501. drm_i915_private_t *dev_priv = dev->dev_private;
  3502. int lists_empty;
  3503. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  3504. list_empty(&dev_priv->mm.active_list);
  3505. return !lists_empty;
  3506. }
  3507. static int
  3508. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3509. {
  3510. struct drm_i915_private *dev_priv =
  3511. container_of(shrinker,
  3512. struct drm_i915_private,
  3513. mm.inactive_shrinker);
  3514. struct drm_device *dev = dev_priv->dev;
  3515. struct drm_i915_gem_object *obj, *next;
  3516. int nr_to_scan = sc->nr_to_scan;
  3517. int cnt;
  3518. if (!mutex_trylock(&dev->struct_mutex))
  3519. return 0;
  3520. /* "fast-path" to count number of available objects */
  3521. if (nr_to_scan == 0) {
  3522. cnt = 0;
  3523. list_for_each_entry(obj,
  3524. &dev_priv->mm.inactive_list,
  3525. mm_list)
  3526. cnt++;
  3527. mutex_unlock(&dev->struct_mutex);
  3528. return cnt / 100 * sysctl_vfs_cache_pressure;
  3529. }
  3530. rescan:
  3531. /* first scan for clean buffers */
  3532. i915_gem_retire_requests(dev);
  3533. list_for_each_entry_safe(obj, next,
  3534. &dev_priv->mm.inactive_list,
  3535. mm_list) {
  3536. if (i915_gem_object_is_purgeable(obj)) {
  3537. if (i915_gem_object_unbind(obj) == 0 &&
  3538. --nr_to_scan == 0)
  3539. break;
  3540. }
  3541. }
  3542. /* second pass, evict/count anything still on the inactive list */
  3543. cnt = 0;
  3544. list_for_each_entry_safe(obj, next,
  3545. &dev_priv->mm.inactive_list,
  3546. mm_list) {
  3547. if (nr_to_scan &&
  3548. i915_gem_object_unbind(obj) == 0)
  3549. nr_to_scan--;
  3550. else
  3551. cnt++;
  3552. }
  3553. if (nr_to_scan && i915_gpu_is_active(dev)) {
  3554. /*
  3555. * We are desperate for pages, so as a last resort, wait
  3556. * for the GPU to finish and discard whatever we can.
  3557. * This has a dramatic impact to reduce the number of
  3558. * OOM-killer events whilst running the GPU aggressively.
  3559. */
  3560. if (i915_gpu_idle(dev, true) == 0)
  3561. goto rescan;
  3562. }
  3563. mutex_unlock(&dev->struct_mutex);
  3564. return cnt / 100 * sysctl_vfs_cache_pressure;
  3565. }