i915_gem.c 120 KB

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