i915_gem.c 115 KB

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