i915_gem.c 115 KB

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