i915_gem.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include <linux/shmem_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-buf.h>
  39. #define RQ_BUG_ON(expr)
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static bool cpu_cache_is_coherent(struct drm_device *dev,
  47. enum i915_cache_level level)
  48. {
  49. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  50. }
  51. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  52. {
  53. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  54. return true;
  55. return obj->pin_display;
  56. }
  57. /* some bookkeeping */
  58. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  59. size_t size)
  60. {
  61. spin_lock(&dev_priv->mm.object_stat_lock);
  62. dev_priv->mm.object_count++;
  63. dev_priv->mm.object_memory += size;
  64. spin_unlock(&dev_priv->mm.object_stat_lock);
  65. }
  66. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  67. size_t size)
  68. {
  69. spin_lock(&dev_priv->mm.object_stat_lock);
  70. dev_priv->mm.object_count--;
  71. dev_priv->mm.object_memory -= size;
  72. spin_unlock(&dev_priv->mm.object_stat_lock);
  73. }
  74. static int
  75. i915_gem_wait_for_error(struct i915_gpu_error *error)
  76. {
  77. int ret;
  78. #define EXIT_COND (!i915_reset_in_progress(error) || \
  79. i915_terminally_wedged(error))
  80. if (EXIT_COND)
  81. return 0;
  82. /*
  83. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  84. * userspace. If it takes that long something really bad is going on and
  85. * we should simply try to bail out and fail as gracefully as possible.
  86. */
  87. ret = wait_event_interruptible_timeout(error->reset_queue,
  88. EXIT_COND,
  89. 10*HZ);
  90. if (ret == 0) {
  91. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  92. return -EIO;
  93. } else if (ret < 0) {
  94. return ret;
  95. }
  96. #undef EXIT_COND
  97. return 0;
  98. }
  99. int i915_mutex_lock_interruptible(struct drm_device *dev)
  100. {
  101. struct drm_i915_private *dev_priv = dev->dev_private;
  102. int ret;
  103. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  104. if (ret)
  105. return ret;
  106. ret = mutex_lock_interruptible(&dev->struct_mutex);
  107. if (ret)
  108. return ret;
  109. WARN_ON(i915_verify_lists(dev));
  110. return 0;
  111. }
  112. int
  113. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  114. struct drm_file *file)
  115. {
  116. struct drm_i915_private *dev_priv = dev->dev_private;
  117. struct drm_i915_gem_get_aperture *args = data;
  118. struct i915_gtt *ggtt = &dev_priv->gtt;
  119. struct i915_vma *vma;
  120. size_t pinned;
  121. pinned = 0;
  122. mutex_lock(&dev->struct_mutex);
  123. list_for_each_entry(vma, &ggtt->base.active_list, mm_list)
  124. if (vma->pin_count)
  125. pinned += vma->node.size;
  126. list_for_each_entry(vma, &ggtt->base.inactive_list, mm_list)
  127. if (vma->pin_count)
  128. pinned += vma->node.size;
  129. mutex_unlock(&dev->struct_mutex);
  130. args->aper_size = dev_priv->gtt.base.total;
  131. args->aper_available_size = args->aper_size - pinned;
  132. return 0;
  133. }
  134. static int
  135. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  136. {
  137. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  138. char *vaddr = obj->phys_handle->vaddr;
  139. struct sg_table *st;
  140. struct scatterlist *sg;
  141. int i;
  142. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  143. return -EINVAL;
  144. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  145. struct page *page;
  146. char *src;
  147. page = shmem_read_mapping_page(mapping, i);
  148. if (IS_ERR(page))
  149. return PTR_ERR(page);
  150. src = kmap_atomic(page);
  151. memcpy(vaddr, src, PAGE_SIZE);
  152. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  153. kunmap_atomic(src);
  154. page_cache_release(page);
  155. vaddr += PAGE_SIZE;
  156. }
  157. i915_gem_chipset_flush(obj->base.dev);
  158. st = kmalloc(sizeof(*st), GFP_KERNEL);
  159. if (st == NULL)
  160. return -ENOMEM;
  161. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  162. kfree(st);
  163. return -ENOMEM;
  164. }
  165. sg = st->sgl;
  166. sg->offset = 0;
  167. sg->length = obj->base.size;
  168. sg_dma_address(sg) = obj->phys_handle->busaddr;
  169. sg_dma_len(sg) = obj->base.size;
  170. obj->pages = st;
  171. return 0;
  172. }
  173. static void
  174. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  175. {
  176. int ret;
  177. BUG_ON(obj->madv == __I915_MADV_PURGED);
  178. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  179. if (ret) {
  180. /* In the event of a disaster, abandon all caches and
  181. * hope for the best.
  182. */
  183. WARN_ON(ret != -EIO);
  184. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  185. }
  186. if (obj->madv == I915_MADV_DONTNEED)
  187. obj->dirty = 0;
  188. if (obj->dirty) {
  189. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  190. char *vaddr = obj->phys_handle->vaddr;
  191. int i;
  192. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  193. struct page *page;
  194. char *dst;
  195. page = shmem_read_mapping_page(mapping, i);
  196. if (IS_ERR(page))
  197. continue;
  198. dst = kmap_atomic(page);
  199. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  200. memcpy(dst, vaddr, PAGE_SIZE);
  201. kunmap_atomic(dst);
  202. set_page_dirty(page);
  203. if (obj->madv == I915_MADV_WILLNEED)
  204. mark_page_accessed(page);
  205. page_cache_release(page);
  206. vaddr += PAGE_SIZE;
  207. }
  208. obj->dirty = 0;
  209. }
  210. sg_free_table(obj->pages);
  211. kfree(obj->pages);
  212. }
  213. static void
  214. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  215. {
  216. drm_pci_free(obj->base.dev, obj->phys_handle);
  217. }
  218. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  219. .get_pages = i915_gem_object_get_pages_phys,
  220. .put_pages = i915_gem_object_put_pages_phys,
  221. .release = i915_gem_object_release_phys,
  222. };
  223. static int
  224. drop_pages(struct drm_i915_gem_object *obj)
  225. {
  226. struct i915_vma *vma, *next;
  227. int ret;
  228. drm_gem_object_reference(&obj->base);
  229. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link)
  230. if (i915_vma_unbind(vma))
  231. break;
  232. ret = i915_gem_object_put_pages(obj);
  233. drm_gem_object_unreference(&obj->base);
  234. return ret;
  235. }
  236. int
  237. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  238. int align)
  239. {
  240. drm_dma_handle_t *phys;
  241. int ret;
  242. if (obj->phys_handle) {
  243. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  244. return -EBUSY;
  245. return 0;
  246. }
  247. if (obj->madv != I915_MADV_WILLNEED)
  248. return -EFAULT;
  249. if (obj->base.filp == NULL)
  250. return -EINVAL;
  251. ret = drop_pages(obj);
  252. if (ret)
  253. return ret;
  254. /* create a new object */
  255. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  256. if (!phys)
  257. return -ENOMEM;
  258. obj->phys_handle = phys;
  259. obj->ops = &i915_gem_phys_ops;
  260. return i915_gem_object_get_pages(obj);
  261. }
  262. static int
  263. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  264. struct drm_i915_gem_pwrite *args,
  265. struct drm_file *file_priv)
  266. {
  267. struct drm_device *dev = obj->base.dev;
  268. void *vaddr = obj->phys_handle->vaddr + args->offset;
  269. char __user *user_data = to_user_ptr(args->data_ptr);
  270. int ret = 0;
  271. /* We manually control the domain here and pretend that it
  272. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  273. */
  274. ret = i915_gem_object_wait_rendering(obj, false);
  275. if (ret)
  276. return ret;
  277. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  278. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  279. unsigned long unwritten;
  280. /* The physical object once assigned is fixed for the lifetime
  281. * of the obj, so we can safely drop the lock and continue
  282. * to access vaddr.
  283. */
  284. mutex_unlock(&dev->struct_mutex);
  285. unwritten = copy_from_user(vaddr, user_data, args->size);
  286. mutex_lock(&dev->struct_mutex);
  287. if (unwritten) {
  288. ret = -EFAULT;
  289. goto out;
  290. }
  291. }
  292. drm_clflush_virt_range(vaddr, args->size);
  293. i915_gem_chipset_flush(dev);
  294. out:
  295. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  296. return ret;
  297. }
  298. void *i915_gem_object_alloc(struct drm_device *dev)
  299. {
  300. struct drm_i915_private *dev_priv = dev->dev_private;
  301. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  302. }
  303. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  304. {
  305. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  306. kmem_cache_free(dev_priv->objects, obj);
  307. }
  308. static int
  309. i915_gem_create(struct drm_file *file,
  310. struct drm_device *dev,
  311. uint64_t size,
  312. uint32_t *handle_p)
  313. {
  314. struct drm_i915_gem_object *obj;
  315. int ret;
  316. u32 handle;
  317. size = roundup(size, PAGE_SIZE);
  318. if (size == 0)
  319. return -EINVAL;
  320. /* Allocate the new object */
  321. obj = i915_gem_alloc_object(dev, size);
  322. if (obj == NULL)
  323. return -ENOMEM;
  324. ret = drm_gem_handle_create(file, &obj->base, &handle);
  325. /* drop reference from allocate - handle holds it now */
  326. drm_gem_object_unreference_unlocked(&obj->base);
  327. if (ret)
  328. return ret;
  329. *handle_p = handle;
  330. return 0;
  331. }
  332. int
  333. i915_gem_dumb_create(struct drm_file *file,
  334. struct drm_device *dev,
  335. struct drm_mode_create_dumb *args)
  336. {
  337. /* have to work out size/pitch and return them */
  338. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  339. args->size = args->pitch * args->height;
  340. return i915_gem_create(file, dev,
  341. args->size, &args->handle);
  342. }
  343. /**
  344. * Creates a new mm object and returns a handle to it.
  345. */
  346. int
  347. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  348. struct drm_file *file)
  349. {
  350. struct drm_i915_gem_create *args = data;
  351. return i915_gem_create(file, dev,
  352. args->size, &args->handle);
  353. }
  354. static inline int
  355. __copy_to_user_swizzled(char __user *cpu_vaddr,
  356. const char *gpu_vaddr, int gpu_offset,
  357. int length)
  358. {
  359. int ret, cpu_offset = 0;
  360. while (length > 0) {
  361. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  362. int this_length = min(cacheline_end - gpu_offset, length);
  363. int swizzled_gpu_offset = gpu_offset ^ 64;
  364. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  365. gpu_vaddr + swizzled_gpu_offset,
  366. this_length);
  367. if (ret)
  368. return ret + length;
  369. cpu_offset += this_length;
  370. gpu_offset += this_length;
  371. length -= this_length;
  372. }
  373. return 0;
  374. }
  375. static inline int
  376. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  377. const char __user *cpu_vaddr,
  378. int length)
  379. {
  380. int ret, cpu_offset = 0;
  381. while (length > 0) {
  382. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  383. int this_length = min(cacheline_end - gpu_offset, length);
  384. int swizzled_gpu_offset = gpu_offset ^ 64;
  385. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  386. cpu_vaddr + cpu_offset,
  387. this_length);
  388. if (ret)
  389. return ret + length;
  390. cpu_offset += this_length;
  391. gpu_offset += this_length;
  392. length -= this_length;
  393. }
  394. return 0;
  395. }
  396. /*
  397. * Pins the specified object's pages and synchronizes the object with
  398. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  399. * flush the object from the CPU cache.
  400. */
  401. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  402. int *needs_clflush)
  403. {
  404. int ret;
  405. *needs_clflush = 0;
  406. if (!obj->base.filp)
  407. return -EINVAL;
  408. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  409. /* If we're not in the cpu read domain, set ourself into the gtt
  410. * read domain and manually flush cachelines (if required). This
  411. * optimizes for the case when the gpu will dirty the data
  412. * anyway again before the next pread happens. */
  413. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  414. obj->cache_level);
  415. ret = i915_gem_object_wait_rendering(obj, true);
  416. if (ret)
  417. return ret;
  418. }
  419. ret = i915_gem_object_get_pages(obj);
  420. if (ret)
  421. return ret;
  422. i915_gem_object_pin_pages(obj);
  423. return ret;
  424. }
  425. /* Per-page copy function for the shmem pread fastpath.
  426. * Flushes invalid cachelines before reading the target if
  427. * needs_clflush is set. */
  428. static int
  429. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  430. char __user *user_data,
  431. bool page_do_bit17_swizzling, bool needs_clflush)
  432. {
  433. char *vaddr;
  434. int ret;
  435. if (unlikely(page_do_bit17_swizzling))
  436. return -EINVAL;
  437. vaddr = kmap_atomic(page);
  438. if (needs_clflush)
  439. drm_clflush_virt_range(vaddr + shmem_page_offset,
  440. page_length);
  441. ret = __copy_to_user_inatomic(user_data,
  442. vaddr + shmem_page_offset,
  443. page_length);
  444. kunmap_atomic(vaddr);
  445. return ret ? -EFAULT : 0;
  446. }
  447. static void
  448. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  449. bool swizzled)
  450. {
  451. if (unlikely(swizzled)) {
  452. unsigned long start = (unsigned long) addr;
  453. unsigned long end = (unsigned long) addr + length;
  454. /* For swizzling simply ensure that we always flush both
  455. * channels. Lame, but simple and it works. Swizzled
  456. * pwrite/pread is far from a hotpath - current userspace
  457. * doesn't use it at all. */
  458. start = round_down(start, 128);
  459. end = round_up(end, 128);
  460. drm_clflush_virt_range((void *)start, end - start);
  461. } else {
  462. drm_clflush_virt_range(addr, length);
  463. }
  464. }
  465. /* Only difference to the fast-path function is that this can handle bit17
  466. * and uses non-atomic copy and kmap functions. */
  467. static int
  468. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  469. char __user *user_data,
  470. bool page_do_bit17_swizzling, bool needs_clflush)
  471. {
  472. char *vaddr;
  473. int ret;
  474. vaddr = kmap(page);
  475. if (needs_clflush)
  476. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  477. page_length,
  478. page_do_bit17_swizzling);
  479. if (page_do_bit17_swizzling)
  480. ret = __copy_to_user_swizzled(user_data,
  481. vaddr, shmem_page_offset,
  482. page_length);
  483. else
  484. ret = __copy_to_user(user_data,
  485. vaddr + shmem_page_offset,
  486. page_length);
  487. kunmap(page);
  488. return ret ? - EFAULT : 0;
  489. }
  490. static int
  491. i915_gem_shmem_pread(struct drm_device *dev,
  492. struct drm_i915_gem_object *obj,
  493. struct drm_i915_gem_pread *args,
  494. struct drm_file *file)
  495. {
  496. char __user *user_data;
  497. ssize_t remain;
  498. loff_t offset;
  499. int shmem_page_offset, page_length, ret = 0;
  500. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  501. int prefaulted = 0;
  502. int needs_clflush = 0;
  503. struct sg_page_iter sg_iter;
  504. user_data = to_user_ptr(args->data_ptr);
  505. remain = args->size;
  506. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  507. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  508. if (ret)
  509. return ret;
  510. offset = args->offset;
  511. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  512. offset >> PAGE_SHIFT) {
  513. struct page *page = sg_page_iter_page(&sg_iter);
  514. if (remain <= 0)
  515. break;
  516. /* Operation in this page
  517. *
  518. * shmem_page_offset = offset within page in shmem file
  519. * page_length = bytes to copy for this page
  520. */
  521. shmem_page_offset = offset_in_page(offset);
  522. page_length = remain;
  523. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  524. page_length = PAGE_SIZE - shmem_page_offset;
  525. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  526. (page_to_phys(page) & (1 << 17)) != 0;
  527. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  528. user_data, page_do_bit17_swizzling,
  529. needs_clflush);
  530. if (ret == 0)
  531. goto next_page;
  532. mutex_unlock(&dev->struct_mutex);
  533. if (likely(!i915.prefault_disable) && !prefaulted) {
  534. ret = fault_in_multipages_writeable(user_data, remain);
  535. /* Userspace is tricking us, but we've already clobbered
  536. * its pages with the prefault and promised to write the
  537. * data up to the first fault. Hence ignore any errors
  538. * and just continue. */
  539. (void)ret;
  540. prefaulted = 1;
  541. }
  542. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  543. user_data, page_do_bit17_swizzling,
  544. needs_clflush);
  545. mutex_lock(&dev->struct_mutex);
  546. if (ret)
  547. goto out;
  548. next_page:
  549. remain -= page_length;
  550. user_data += page_length;
  551. offset += page_length;
  552. }
  553. out:
  554. i915_gem_object_unpin_pages(obj);
  555. return ret;
  556. }
  557. /**
  558. * Reads data from the object referenced by handle.
  559. *
  560. * On error, the contents of *data are undefined.
  561. */
  562. int
  563. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  564. struct drm_file *file)
  565. {
  566. struct drm_i915_gem_pread *args = data;
  567. struct drm_i915_gem_object *obj;
  568. int ret = 0;
  569. if (args->size == 0)
  570. return 0;
  571. if (!access_ok(VERIFY_WRITE,
  572. to_user_ptr(args->data_ptr),
  573. args->size))
  574. return -EFAULT;
  575. ret = i915_mutex_lock_interruptible(dev);
  576. if (ret)
  577. return ret;
  578. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  579. if (&obj->base == NULL) {
  580. ret = -ENOENT;
  581. goto unlock;
  582. }
  583. /* Bounds check source. */
  584. if (args->offset > obj->base.size ||
  585. args->size > obj->base.size - args->offset) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. /* prime objects have no backing filp to GEM pread/pwrite
  590. * pages from.
  591. */
  592. if (!obj->base.filp) {
  593. ret = -EINVAL;
  594. goto out;
  595. }
  596. trace_i915_gem_object_pread(obj, args->offset, args->size);
  597. ret = i915_gem_shmem_pread(dev, obj, args, file);
  598. out:
  599. drm_gem_object_unreference(&obj->base);
  600. unlock:
  601. mutex_unlock(&dev->struct_mutex);
  602. return ret;
  603. }
  604. /* This is the fast write path which cannot handle
  605. * page faults in the source data
  606. */
  607. static inline int
  608. fast_user_write(struct io_mapping *mapping,
  609. loff_t page_base, int page_offset,
  610. char __user *user_data,
  611. int length)
  612. {
  613. void __iomem *vaddr_atomic;
  614. void *vaddr;
  615. unsigned long unwritten;
  616. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  617. /* We can use the cpu mem copy function because this is X86. */
  618. vaddr = (void __force*)vaddr_atomic + page_offset;
  619. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  620. user_data, length);
  621. io_mapping_unmap_atomic(vaddr_atomic);
  622. return unwritten;
  623. }
  624. /**
  625. * This is the fast pwrite path, where we copy the data directly from the
  626. * user into the GTT, uncached.
  627. */
  628. static int
  629. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  630. struct drm_i915_gem_object *obj,
  631. struct drm_i915_gem_pwrite *args,
  632. struct drm_file *file)
  633. {
  634. struct drm_i915_private *dev_priv = dev->dev_private;
  635. ssize_t remain;
  636. loff_t offset, page_base;
  637. char __user *user_data;
  638. int page_offset, page_length, ret;
  639. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  640. if (ret)
  641. goto out;
  642. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  643. if (ret)
  644. goto out_unpin;
  645. ret = i915_gem_object_put_fence(obj);
  646. if (ret)
  647. goto out_unpin;
  648. user_data = to_user_ptr(args->data_ptr);
  649. remain = args->size;
  650. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  651. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  652. while (remain > 0) {
  653. /* Operation in this page
  654. *
  655. * page_base = page offset within aperture
  656. * page_offset = offset within page
  657. * page_length = bytes to copy for this page
  658. */
  659. page_base = offset & PAGE_MASK;
  660. page_offset = offset_in_page(offset);
  661. page_length = remain;
  662. if ((page_offset + remain) > PAGE_SIZE)
  663. page_length = PAGE_SIZE - page_offset;
  664. /* If we get a fault while copying data, then (presumably) our
  665. * source page isn't available. Return the error and we'll
  666. * retry in the slow path.
  667. */
  668. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  669. page_offset, user_data, page_length)) {
  670. ret = -EFAULT;
  671. goto out_flush;
  672. }
  673. remain -= page_length;
  674. user_data += page_length;
  675. offset += page_length;
  676. }
  677. out_flush:
  678. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  679. out_unpin:
  680. i915_gem_object_ggtt_unpin(obj);
  681. out:
  682. return ret;
  683. }
  684. /* Per-page copy function for the shmem pwrite fastpath.
  685. * Flushes invalid cachelines before writing to the target if
  686. * needs_clflush_before is set and flushes out any written cachelines after
  687. * writing if needs_clflush is set. */
  688. static int
  689. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  690. char __user *user_data,
  691. bool page_do_bit17_swizzling,
  692. bool needs_clflush_before,
  693. bool needs_clflush_after)
  694. {
  695. char *vaddr;
  696. int ret;
  697. if (unlikely(page_do_bit17_swizzling))
  698. return -EINVAL;
  699. vaddr = kmap_atomic(page);
  700. if (needs_clflush_before)
  701. drm_clflush_virt_range(vaddr + shmem_page_offset,
  702. page_length);
  703. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  704. user_data, page_length);
  705. if (needs_clflush_after)
  706. drm_clflush_virt_range(vaddr + shmem_page_offset,
  707. page_length);
  708. kunmap_atomic(vaddr);
  709. return ret ? -EFAULT : 0;
  710. }
  711. /* Only difference to the fast-path function is that this can handle bit17
  712. * and uses non-atomic copy and kmap functions. */
  713. static int
  714. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  715. char __user *user_data,
  716. bool page_do_bit17_swizzling,
  717. bool needs_clflush_before,
  718. bool needs_clflush_after)
  719. {
  720. char *vaddr;
  721. int ret;
  722. vaddr = kmap(page);
  723. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  724. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  725. page_length,
  726. page_do_bit17_swizzling);
  727. if (page_do_bit17_swizzling)
  728. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  729. user_data,
  730. page_length);
  731. else
  732. ret = __copy_from_user(vaddr + shmem_page_offset,
  733. user_data,
  734. page_length);
  735. if (needs_clflush_after)
  736. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  737. page_length,
  738. page_do_bit17_swizzling);
  739. kunmap(page);
  740. return ret ? -EFAULT : 0;
  741. }
  742. static int
  743. i915_gem_shmem_pwrite(struct drm_device *dev,
  744. struct drm_i915_gem_object *obj,
  745. struct drm_i915_gem_pwrite *args,
  746. struct drm_file *file)
  747. {
  748. ssize_t remain;
  749. loff_t offset;
  750. char __user *user_data;
  751. int shmem_page_offset, page_length, ret = 0;
  752. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  753. int hit_slowpath = 0;
  754. int needs_clflush_after = 0;
  755. int needs_clflush_before = 0;
  756. struct sg_page_iter sg_iter;
  757. user_data = to_user_ptr(args->data_ptr);
  758. remain = args->size;
  759. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  760. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  761. /* If we're not in the cpu write domain, set ourself into the gtt
  762. * write domain and manually flush cachelines (if required). This
  763. * optimizes for the case when the gpu will use the data
  764. * right away and we therefore have to clflush anyway. */
  765. needs_clflush_after = cpu_write_needs_clflush(obj);
  766. ret = i915_gem_object_wait_rendering(obj, false);
  767. if (ret)
  768. return ret;
  769. }
  770. /* Same trick applies to invalidate partially written cachelines read
  771. * before writing. */
  772. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  773. needs_clflush_before =
  774. !cpu_cache_is_coherent(dev, obj->cache_level);
  775. ret = i915_gem_object_get_pages(obj);
  776. if (ret)
  777. return ret;
  778. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  779. i915_gem_object_pin_pages(obj);
  780. offset = args->offset;
  781. obj->dirty = 1;
  782. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  783. offset >> PAGE_SHIFT) {
  784. struct page *page = sg_page_iter_page(&sg_iter);
  785. int partial_cacheline_write;
  786. if (remain <= 0)
  787. break;
  788. /* Operation in this page
  789. *
  790. * shmem_page_offset = offset within page in shmem file
  791. * page_length = bytes to copy for this page
  792. */
  793. shmem_page_offset = offset_in_page(offset);
  794. page_length = remain;
  795. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  796. page_length = PAGE_SIZE - shmem_page_offset;
  797. /* If we don't overwrite a cacheline completely we need to be
  798. * careful to have up-to-date data by first clflushing. Don't
  799. * overcomplicate things and flush the entire patch. */
  800. partial_cacheline_write = needs_clflush_before &&
  801. ((shmem_page_offset | page_length)
  802. & (boot_cpu_data.x86_clflush_size - 1));
  803. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  804. (page_to_phys(page) & (1 << 17)) != 0;
  805. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  806. user_data, page_do_bit17_swizzling,
  807. partial_cacheline_write,
  808. needs_clflush_after);
  809. if (ret == 0)
  810. goto next_page;
  811. hit_slowpath = 1;
  812. mutex_unlock(&dev->struct_mutex);
  813. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  814. user_data, page_do_bit17_swizzling,
  815. partial_cacheline_write,
  816. needs_clflush_after);
  817. mutex_lock(&dev->struct_mutex);
  818. if (ret)
  819. goto out;
  820. next_page:
  821. remain -= page_length;
  822. user_data += page_length;
  823. offset += page_length;
  824. }
  825. out:
  826. i915_gem_object_unpin_pages(obj);
  827. if (hit_slowpath) {
  828. /*
  829. * Fixup: Flush cpu caches in case we didn't flush the dirty
  830. * cachelines in-line while writing and the object moved
  831. * out of the cpu write domain while we've dropped the lock.
  832. */
  833. if (!needs_clflush_after &&
  834. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  835. if (i915_gem_clflush_object(obj, obj->pin_display))
  836. needs_clflush_after = true;
  837. }
  838. }
  839. if (needs_clflush_after)
  840. i915_gem_chipset_flush(dev);
  841. else
  842. obj->cache_dirty = true;
  843. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  844. return ret;
  845. }
  846. /**
  847. * Writes data to the object referenced by handle.
  848. *
  849. * On error, the contents of the buffer that were to be modified are undefined.
  850. */
  851. int
  852. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  853. struct drm_file *file)
  854. {
  855. struct drm_i915_private *dev_priv = dev->dev_private;
  856. struct drm_i915_gem_pwrite *args = data;
  857. struct drm_i915_gem_object *obj;
  858. int ret;
  859. if (args->size == 0)
  860. return 0;
  861. if (!access_ok(VERIFY_READ,
  862. to_user_ptr(args->data_ptr),
  863. args->size))
  864. return -EFAULT;
  865. if (likely(!i915.prefault_disable)) {
  866. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  867. args->size);
  868. if (ret)
  869. return -EFAULT;
  870. }
  871. intel_runtime_pm_get(dev_priv);
  872. ret = i915_mutex_lock_interruptible(dev);
  873. if (ret)
  874. goto put_rpm;
  875. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  876. if (&obj->base == NULL) {
  877. ret = -ENOENT;
  878. goto unlock;
  879. }
  880. /* Bounds check destination. */
  881. if (args->offset > obj->base.size ||
  882. args->size > obj->base.size - args->offset) {
  883. ret = -EINVAL;
  884. goto out;
  885. }
  886. /* prime objects have no backing filp to GEM pread/pwrite
  887. * pages from.
  888. */
  889. if (!obj->base.filp) {
  890. ret = -EINVAL;
  891. goto out;
  892. }
  893. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  894. ret = -EFAULT;
  895. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  896. * it would end up going through the fenced access, and we'll get
  897. * different detiling behavior between reading and writing.
  898. * pread/pwrite currently are reading and writing from the CPU
  899. * perspective, requiring manual detiling by the client.
  900. */
  901. if (obj->tiling_mode == I915_TILING_NONE &&
  902. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  903. cpu_write_needs_clflush(obj)) {
  904. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  905. /* Note that the gtt paths might fail with non-page-backed user
  906. * pointers (e.g. gtt mappings when moving data between
  907. * textures). Fallback to the shmem path in that case. */
  908. }
  909. if (ret == -EFAULT || ret == -ENOSPC) {
  910. if (obj->phys_handle)
  911. ret = i915_gem_phys_pwrite(obj, args, file);
  912. else
  913. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  914. }
  915. out:
  916. drm_gem_object_unreference(&obj->base);
  917. unlock:
  918. mutex_unlock(&dev->struct_mutex);
  919. put_rpm:
  920. intel_runtime_pm_put(dev_priv);
  921. return ret;
  922. }
  923. int
  924. i915_gem_check_wedge(struct i915_gpu_error *error,
  925. bool interruptible)
  926. {
  927. if (i915_reset_in_progress(error)) {
  928. /* Non-interruptible callers can't handle -EAGAIN, hence return
  929. * -EIO unconditionally for these. */
  930. if (!interruptible)
  931. return -EIO;
  932. /* Recovery complete, but the reset failed ... */
  933. if (i915_terminally_wedged(error))
  934. return -EIO;
  935. /*
  936. * Check if GPU Reset is in progress - we need intel_ring_begin
  937. * to work properly to reinit the hw state while the gpu is
  938. * still marked as reset-in-progress. Handle this with a flag.
  939. */
  940. if (!error->reload_in_reset)
  941. return -EAGAIN;
  942. }
  943. return 0;
  944. }
  945. static void fake_irq(unsigned long data)
  946. {
  947. wake_up_process((struct task_struct *)data);
  948. }
  949. static bool missed_irq(struct drm_i915_private *dev_priv,
  950. struct intel_engine_cs *ring)
  951. {
  952. return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  953. }
  954. static int __i915_spin_request(struct drm_i915_gem_request *req)
  955. {
  956. unsigned long timeout;
  957. if (i915_gem_request_get_ring(req)->irq_refcount)
  958. return -EBUSY;
  959. timeout = jiffies + 1;
  960. while (!need_resched()) {
  961. if (i915_gem_request_completed(req, true))
  962. return 0;
  963. if (time_after_eq(jiffies, timeout))
  964. break;
  965. cpu_relax_lowlatency();
  966. }
  967. if (i915_gem_request_completed(req, false))
  968. return 0;
  969. return -EAGAIN;
  970. }
  971. /**
  972. * __i915_wait_request - wait until execution of request has finished
  973. * @req: duh!
  974. * @reset_counter: reset sequence associated with the given request
  975. * @interruptible: do an interruptible wait (normally yes)
  976. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  977. *
  978. * Note: It is of utmost importance that the passed in seqno and reset_counter
  979. * values have been read by the caller in an smp safe manner. Where read-side
  980. * locks are involved, it is sufficient to read the reset_counter before
  981. * unlocking the lock that protects the seqno. For lockless tricks, the
  982. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  983. * inserted.
  984. *
  985. * Returns 0 if the request was found within the alloted time. Else returns the
  986. * errno with remaining time filled in timeout argument.
  987. */
  988. int __i915_wait_request(struct drm_i915_gem_request *req,
  989. unsigned reset_counter,
  990. bool interruptible,
  991. s64 *timeout,
  992. struct intel_rps_client *rps)
  993. {
  994. struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  995. struct drm_device *dev = ring->dev;
  996. struct drm_i915_private *dev_priv = dev->dev_private;
  997. const bool irq_test_in_progress =
  998. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  999. DEFINE_WAIT(wait);
  1000. unsigned long timeout_expire;
  1001. s64 before, now;
  1002. int ret;
  1003. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1004. if (list_empty(&req->list))
  1005. return 0;
  1006. if (i915_gem_request_completed(req, true))
  1007. return 0;
  1008. timeout_expire = 0;
  1009. if (timeout) {
  1010. if (WARN_ON(*timeout < 0))
  1011. return -EINVAL;
  1012. if (*timeout == 0)
  1013. return -ETIME;
  1014. timeout_expire = jiffies + nsecs_to_jiffies_timeout(*timeout);
  1015. }
  1016. if (INTEL_INFO(dev_priv)->gen >= 6)
  1017. gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
  1018. /* Record current time in case interrupted by signal, or wedged */
  1019. trace_i915_gem_request_wait_begin(req);
  1020. before = ktime_get_raw_ns();
  1021. /* Optimistic spin for the next jiffie before touching IRQs */
  1022. ret = __i915_spin_request(req);
  1023. if (ret == 0)
  1024. goto out;
  1025. if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring))) {
  1026. ret = -ENODEV;
  1027. goto out;
  1028. }
  1029. for (;;) {
  1030. struct timer_list timer;
  1031. prepare_to_wait(&ring->irq_queue, &wait,
  1032. interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  1033. /* We need to check whether any gpu reset happened in between
  1034. * the caller grabbing the seqno and now ... */
  1035. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  1036. /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  1037. * is truely gone. */
  1038. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1039. if (ret == 0)
  1040. ret = -EAGAIN;
  1041. break;
  1042. }
  1043. if (i915_gem_request_completed(req, false)) {
  1044. ret = 0;
  1045. break;
  1046. }
  1047. if (interruptible && signal_pending(current)) {
  1048. ret = -ERESTARTSYS;
  1049. break;
  1050. }
  1051. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1052. ret = -ETIME;
  1053. break;
  1054. }
  1055. timer.function = NULL;
  1056. if (timeout || missed_irq(dev_priv, ring)) {
  1057. unsigned long expire;
  1058. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1059. expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
  1060. mod_timer(&timer, expire);
  1061. }
  1062. io_schedule();
  1063. if (timer.function) {
  1064. del_singleshot_timer_sync(&timer);
  1065. destroy_timer_on_stack(&timer);
  1066. }
  1067. }
  1068. if (!irq_test_in_progress)
  1069. ring->irq_put(ring);
  1070. finish_wait(&ring->irq_queue, &wait);
  1071. out:
  1072. now = ktime_get_raw_ns();
  1073. trace_i915_gem_request_wait_end(req);
  1074. if (timeout) {
  1075. s64 tres = *timeout - (now - before);
  1076. *timeout = tres < 0 ? 0 : tres;
  1077. /*
  1078. * Apparently ktime isn't accurate enough and occasionally has a
  1079. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1080. * things up to make the test happy. We allow up to 1 jiffy.
  1081. *
  1082. * This is a regrssion from the timespec->ktime conversion.
  1083. */
  1084. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1085. *timeout = 0;
  1086. }
  1087. return ret;
  1088. }
  1089. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  1090. struct drm_file *file)
  1091. {
  1092. struct drm_i915_private *dev_private;
  1093. struct drm_i915_file_private *file_priv;
  1094. WARN_ON(!req || !file || req->file_priv);
  1095. if (!req || !file)
  1096. return -EINVAL;
  1097. if (req->file_priv)
  1098. return -EINVAL;
  1099. dev_private = req->ring->dev->dev_private;
  1100. file_priv = file->driver_priv;
  1101. spin_lock(&file_priv->mm.lock);
  1102. req->file_priv = file_priv;
  1103. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  1104. spin_unlock(&file_priv->mm.lock);
  1105. req->pid = get_pid(task_pid(current));
  1106. return 0;
  1107. }
  1108. static inline void
  1109. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1110. {
  1111. struct drm_i915_file_private *file_priv = request->file_priv;
  1112. if (!file_priv)
  1113. return;
  1114. spin_lock(&file_priv->mm.lock);
  1115. list_del(&request->client_list);
  1116. request->file_priv = NULL;
  1117. spin_unlock(&file_priv->mm.lock);
  1118. put_pid(request->pid);
  1119. request->pid = NULL;
  1120. }
  1121. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1122. {
  1123. trace_i915_gem_request_retire(request);
  1124. /* We know the GPU must have read the request to have
  1125. * sent us the seqno + interrupt, so use the position
  1126. * of tail of the request to update the last known position
  1127. * of the GPU head.
  1128. *
  1129. * Note this requires that we are always called in request
  1130. * completion order.
  1131. */
  1132. request->ringbuf->last_retired_head = request->postfix;
  1133. list_del_init(&request->list);
  1134. i915_gem_request_remove_from_client(request);
  1135. i915_gem_request_unreference(request);
  1136. }
  1137. static void
  1138. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1139. {
  1140. struct intel_engine_cs *engine = req->ring;
  1141. struct drm_i915_gem_request *tmp;
  1142. lockdep_assert_held(&engine->dev->struct_mutex);
  1143. if (list_empty(&req->list))
  1144. return;
  1145. do {
  1146. tmp = list_first_entry(&engine->request_list,
  1147. typeof(*tmp), list);
  1148. i915_gem_request_retire(tmp);
  1149. } while (tmp != req);
  1150. WARN_ON(i915_verify_lists(engine->dev));
  1151. }
  1152. /**
  1153. * Waits for a request to be signaled, and cleans up the
  1154. * request and object lists appropriately for that event.
  1155. */
  1156. int
  1157. i915_wait_request(struct drm_i915_gem_request *req)
  1158. {
  1159. struct drm_device *dev;
  1160. struct drm_i915_private *dev_priv;
  1161. bool interruptible;
  1162. int ret;
  1163. BUG_ON(req == NULL);
  1164. dev = req->ring->dev;
  1165. dev_priv = dev->dev_private;
  1166. interruptible = dev_priv->mm.interruptible;
  1167. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1168. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1169. if (ret)
  1170. return ret;
  1171. ret = __i915_wait_request(req,
  1172. atomic_read(&dev_priv->gpu_error.reset_counter),
  1173. interruptible, NULL, NULL);
  1174. if (ret)
  1175. return ret;
  1176. __i915_gem_request_retire__upto(req);
  1177. return 0;
  1178. }
  1179. /**
  1180. * Ensures that all rendering to the object has completed and the object is
  1181. * safe to unbind from the GTT or access from the CPU.
  1182. */
  1183. int
  1184. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1185. bool readonly)
  1186. {
  1187. int ret, i;
  1188. if (!obj->active)
  1189. return 0;
  1190. if (readonly) {
  1191. if (obj->last_write_req != NULL) {
  1192. ret = i915_wait_request(obj->last_write_req);
  1193. if (ret)
  1194. return ret;
  1195. i = obj->last_write_req->ring->id;
  1196. if (obj->last_read_req[i] == obj->last_write_req)
  1197. i915_gem_object_retire__read(obj, i);
  1198. else
  1199. i915_gem_object_retire__write(obj);
  1200. }
  1201. } else {
  1202. for (i = 0; i < I915_NUM_RINGS; i++) {
  1203. if (obj->last_read_req[i] == NULL)
  1204. continue;
  1205. ret = i915_wait_request(obj->last_read_req[i]);
  1206. if (ret)
  1207. return ret;
  1208. i915_gem_object_retire__read(obj, i);
  1209. }
  1210. RQ_BUG_ON(obj->active);
  1211. }
  1212. return 0;
  1213. }
  1214. static void
  1215. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1216. struct drm_i915_gem_request *req)
  1217. {
  1218. int ring = req->ring->id;
  1219. if (obj->last_read_req[ring] == req)
  1220. i915_gem_object_retire__read(obj, ring);
  1221. else if (obj->last_write_req == req)
  1222. i915_gem_object_retire__write(obj);
  1223. __i915_gem_request_retire__upto(req);
  1224. }
  1225. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1226. * as the object state may change during this call.
  1227. */
  1228. static __must_check int
  1229. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1230. struct intel_rps_client *rps,
  1231. bool readonly)
  1232. {
  1233. struct drm_device *dev = obj->base.dev;
  1234. struct drm_i915_private *dev_priv = dev->dev_private;
  1235. struct drm_i915_gem_request *requests[I915_NUM_RINGS];
  1236. unsigned reset_counter;
  1237. int ret, i, n = 0;
  1238. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1239. BUG_ON(!dev_priv->mm.interruptible);
  1240. if (!obj->active)
  1241. return 0;
  1242. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1243. if (ret)
  1244. return ret;
  1245. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1246. if (readonly) {
  1247. struct drm_i915_gem_request *req;
  1248. req = obj->last_write_req;
  1249. if (req == NULL)
  1250. return 0;
  1251. requests[n++] = i915_gem_request_reference(req);
  1252. } else {
  1253. for (i = 0; i < I915_NUM_RINGS; i++) {
  1254. struct drm_i915_gem_request *req;
  1255. req = obj->last_read_req[i];
  1256. if (req == NULL)
  1257. continue;
  1258. requests[n++] = i915_gem_request_reference(req);
  1259. }
  1260. }
  1261. mutex_unlock(&dev->struct_mutex);
  1262. for (i = 0; ret == 0 && i < n; i++)
  1263. ret = __i915_wait_request(requests[i], reset_counter, true,
  1264. NULL, rps);
  1265. mutex_lock(&dev->struct_mutex);
  1266. for (i = 0; i < n; i++) {
  1267. if (ret == 0)
  1268. i915_gem_object_retire_request(obj, requests[i]);
  1269. i915_gem_request_unreference(requests[i]);
  1270. }
  1271. return ret;
  1272. }
  1273. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1274. {
  1275. struct drm_i915_file_private *fpriv = file->driver_priv;
  1276. return &fpriv->rps;
  1277. }
  1278. /**
  1279. * Called when user space prepares to use an object with the CPU, either
  1280. * through the mmap ioctl's mapping or a GTT mapping.
  1281. */
  1282. int
  1283. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1284. struct drm_file *file)
  1285. {
  1286. struct drm_i915_gem_set_domain *args = data;
  1287. struct drm_i915_gem_object *obj;
  1288. uint32_t read_domains = args->read_domains;
  1289. uint32_t write_domain = args->write_domain;
  1290. int ret;
  1291. /* Only handle setting domains to types used by the CPU. */
  1292. if (write_domain & I915_GEM_GPU_DOMAINS)
  1293. return -EINVAL;
  1294. if (read_domains & I915_GEM_GPU_DOMAINS)
  1295. return -EINVAL;
  1296. /* Having something in the write domain implies it's in the read
  1297. * domain, and only that read domain. Enforce that in the request.
  1298. */
  1299. if (write_domain != 0 && read_domains != write_domain)
  1300. return -EINVAL;
  1301. ret = i915_mutex_lock_interruptible(dev);
  1302. if (ret)
  1303. return ret;
  1304. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1305. if (&obj->base == NULL) {
  1306. ret = -ENOENT;
  1307. goto unlock;
  1308. }
  1309. /* Try to flush the object off the GPU without holding the lock.
  1310. * We will repeat the flush holding the lock in the normal manner
  1311. * to catch cases where we are gazumped.
  1312. */
  1313. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1314. to_rps_client(file),
  1315. !write_domain);
  1316. if (ret)
  1317. goto unref;
  1318. if (read_domains & I915_GEM_DOMAIN_GTT)
  1319. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1320. else
  1321. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1322. if (write_domain != 0)
  1323. intel_fb_obj_invalidate(obj,
  1324. write_domain == I915_GEM_DOMAIN_GTT ?
  1325. ORIGIN_GTT : ORIGIN_CPU);
  1326. unref:
  1327. drm_gem_object_unreference(&obj->base);
  1328. unlock:
  1329. mutex_unlock(&dev->struct_mutex);
  1330. return ret;
  1331. }
  1332. /**
  1333. * Called when user space has done writes to this buffer
  1334. */
  1335. int
  1336. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1337. struct drm_file *file)
  1338. {
  1339. struct drm_i915_gem_sw_finish *args = data;
  1340. struct drm_i915_gem_object *obj;
  1341. int ret = 0;
  1342. ret = i915_mutex_lock_interruptible(dev);
  1343. if (ret)
  1344. return ret;
  1345. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1346. if (&obj->base == NULL) {
  1347. ret = -ENOENT;
  1348. goto unlock;
  1349. }
  1350. /* Pinned buffers may be scanout, so flush the cache */
  1351. if (obj->pin_display)
  1352. i915_gem_object_flush_cpu_write_domain(obj);
  1353. drm_gem_object_unreference(&obj->base);
  1354. unlock:
  1355. mutex_unlock(&dev->struct_mutex);
  1356. return ret;
  1357. }
  1358. /**
  1359. * Maps the contents of an object, returning the address it is mapped
  1360. * into.
  1361. *
  1362. * While the mapping holds a reference on the contents of the object, it doesn't
  1363. * imply a ref on the object itself.
  1364. *
  1365. * IMPORTANT:
  1366. *
  1367. * DRM driver writers who look a this function as an example for how to do GEM
  1368. * mmap support, please don't implement mmap support like here. The modern way
  1369. * to implement DRM mmap support is with an mmap offset ioctl (like
  1370. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1371. * That way debug tooling like valgrind will understand what's going on, hiding
  1372. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1373. * does cpu mmaps this way because we didn't know better.
  1374. */
  1375. int
  1376. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1377. struct drm_file *file)
  1378. {
  1379. struct drm_i915_gem_mmap *args = data;
  1380. struct drm_gem_object *obj;
  1381. unsigned long addr;
  1382. if (args->flags & ~(I915_MMAP_WC))
  1383. return -EINVAL;
  1384. if (args->flags & I915_MMAP_WC && !cpu_has_pat)
  1385. return -ENODEV;
  1386. obj = drm_gem_object_lookup(dev, file, args->handle);
  1387. if (obj == NULL)
  1388. return -ENOENT;
  1389. /* prime objects have no backing filp to GEM mmap
  1390. * pages from.
  1391. */
  1392. if (!obj->filp) {
  1393. drm_gem_object_unreference_unlocked(obj);
  1394. return -EINVAL;
  1395. }
  1396. addr = vm_mmap(obj->filp, 0, args->size,
  1397. PROT_READ | PROT_WRITE, MAP_SHARED,
  1398. args->offset);
  1399. if (args->flags & I915_MMAP_WC) {
  1400. struct mm_struct *mm = current->mm;
  1401. struct vm_area_struct *vma;
  1402. down_write(&mm->mmap_sem);
  1403. vma = find_vma(mm, addr);
  1404. if (vma)
  1405. vma->vm_page_prot =
  1406. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1407. else
  1408. addr = -ENOMEM;
  1409. up_write(&mm->mmap_sem);
  1410. }
  1411. drm_gem_object_unreference_unlocked(obj);
  1412. if (IS_ERR((void *)addr))
  1413. return addr;
  1414. args->addr_ptr = (uint64_t) addr;
  1415. return 0;
  1416. }
  1417. /**
  1418. * i915_gem_fault - fault a page into the GTT
  1419. * @vma: VMA in question
  1420. * @vmf: fault info
  1421. *
  1422. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1423. * from userspace. The fault handler takes care of binding the object to
  1424. * the GTT (if needed), allocating and programming a fence register (again,
  1425. * only if needed based on whether the old reg is still valid or the object
  1426. * is tiled) and inserting a new PTE into the faulting process.
  1427. *
  1428. * Note that the faulting process may involve evicting existing objects
  1429. * from the GTT and/or fence registers to make room. So performance may
  1430. * suffer if the GTT working set is large or there are few fence registers
  1431. * left.
  1432. */
  1433. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1434. {
  1435. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1436. struct drm_device *dev = obj->base.dev;
  1437. struct drm_i915_private *dev_priv = dev->dev_private;
  1438. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1439. pgoff_t page_offset;
  1440. unsigned long pfn;
  1441. int ret = 0;
  1442. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1443. intel_runtime_pm_get(dev_priv);
  1444. /* We don't use vmf->pgoff since that has the fake offset */
  1445. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1446. PAGE_SHIFT;
  1447. ret = i915_mutex_lock_interruptible(dev);
  1448. if (ret)
  1449. goto out;
  1450. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1451. /* Try to flush the object off the GPU first without holding the lock.
  1452. * Upon reacquiring the lock, we will perform our sanity checks and then
  1453. * repeat the flush holding the lock in the normal manner to catch cases
  1454. * where we are gazumped.
  1455. */
  1456. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1457. if (ret)
  1458. goto unlock;
  1459. /* Access to snoopable pages through the GTT is incoherent. */
  1460. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1461. ret = -EFAULT;
  1462. goto unlock;
  1463. }
  1464. /* Use a partial view if the object is bigger than the aperture. */
  1465. if (obj->base.size >= dev_priv->gtt.mappable_end &&
  1466. obj->tiling_mode == I915_TILING_NONE) {
  1467. static const unsigned int chunk_size = 256; // 1 MiB
  1468. memset(&view, 0, sizeof(view));
  1469. view.type = I915_GGTT_VIEW_PARTIAL;
  1470. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1471. view.params.partial.size =
  1472. min_t(unsigned int,
  1473. chunk_size,
  1474. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1475. view.params.partial.offset);
  1476. }
  1477. /* Now pin it into the GTT if needed */
  1478. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1479. if (ret)
  1480. goto unlock;
  1481. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1482. if (ret)
  1483. goto unpin;
  1484. ret = i915_gem_object_get_fence(obj);
  1485. if (ret)
  1486. goto unpin;
  1487. /* Finally, remap it using the new GTT offset */
  1488. pfn = dev_priv->gtt.mappable_base +
  1489. i915_gem_obj_ggtt_offset_view(obj, &view);
  1490. pfn >>= PAGE_SHIFT;
  1491. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1492. /* Overriding existing pages in partial view does not cause
  1493. * us any trouble as TLBs are still valid because the fault
  1494. * is due to userspace losing part of the mapping or never
  1495. * having accessed it before (at this partials' range).
  1496. */
  1497. unsigned long base = vma->vm_start +
  1498. (view.params.partial.offset << PAGE_SHIFT);
  1499. unsigned int i;
  1500. for (i = 0; i < view.params.partial.size; i++) {
  1501. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1502. if (ret)
  1503. break;
  1504. }
  1505. obj->fault_mappable = true;
  1506. } else {
  1507. if (!obj->fault_mappable) {
  1508. unsigned long size = min_t(unsigned long,
  1509. vma->vm_end - vma->vm_start,
  1510. obj->base.size);
  1511. int i;
  1512. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1513. ret = vm_insert_pfn(vma,
  1514. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1515. pfn + i);
  1516. if (ret)
  1517. break;
  1518. }
  1519. obj->fault_mappable = true;
  1520. } else
  1521. ret = vm_insert_pfn(vma,
  1522. (unsigned long)vmf->virtual_address,
  1523. pfn + page_offset);
  1524. }
  1525. unpin:
  1526. i915_gem_object_ggtt_unpin_view(obj, &view);
  1527. unlock:
  1528. mutex_unlock(&dev->struct_mutex);
  1529. out:
  1530. switch (ret) {
  1531. case -EIO:
  1532. /*
  1533. * We eat errors when the gpu is terminally wedged to avoid
  1534. * userspace unduly crashing (gl has no provisions for mmaps to
  1535. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1536. * and so needs to be reported.
  1537. */
  1538. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1539. ret = VM_FAULT_SIGBUS;
  1540. break;
  1541. }
  1542. case -EAGAIN:
  1543. /*
  1544. * EAGAIN means the gpu is hung and we'll wait for the error
  1545. * handler to reset everything when re-faulting in
  1546. * i915_mutex_lock_interruptible.
  1547. */
  1548. case 0:
  1549. case -ERESTARTSYS:
  1550. case -EINTR:
  1551. case -EBUSY:
  1552. /*
  1553. * EBUSY is ok: this just means that another thread
  1554. * already did the job.
  1555. */
  1556. ret = VM_FAULT_NOPAGE;
  1557. break;
  1558. case -ENOMEM:
  1559. ret = VM_FAULT_OOM;
  1560. break;
  1561. case -ENOSPC:
  1562. case -EFAULT:
  1563. ret = VM_FAULT_SIGBUS;
  1564. break;
  1565. default:
  1566. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1567. ret = VM_FAULT_SIGBUS;
  1568. break;
  1569. }
  1570. intel_runtime_pm_put(dev_priv);
  1571. return ret;
  1572. }
  1573. /**
  1574. * i915_gem_release_mmap - remove physical page mappings
  1575. * @obj: obj in question
  1576. *
  1577. * Preserve the reservation of the mmapping with the DRM core code, but
  1578. * relinquish ownership of the pages back to the system.
  1579. *
  1580. * It is vital that we remove the page mapping if we have mapped a tiled
  1581. * object through the GTT and then lose the fence register due to
  1582. * resource pressure. Similarly if the object has been moved out of the
  1583. * aperture, than pages mapped into userspace must be revoked. Removing the
  1584. * mapping will then trigger a page fault on the next user access, allowing
  1585. * fixup by i915_gem_fault().
  1586. */
  1587. void
  1588. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1589. {
  1590. if (!obj->fault_mappable)
  1591. return;
  1592. drm_vma_node_unmap(&obj->base.vma_node,
  1593. obj->base.dev->anon_inode->i_mapping);
  1594. obj->fault_mappable = false;
  1595. }
  1596. void
  1597. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1598. {
  1599. struct drm_i915_gem_object *obj;
  1600. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1601. i915_gem_release_mmap(obj);
  1602. }
  1603. uint32_t
  1604. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1605. {
  1606. uint32_t gtt_size;
  1607. if (INTEL_INFO(dev)->gen >= 4 ||
  1608. tiling_mode == I915_TILING_NONE)
  1609. return size;
  1610. /* Previous chips need a power-of-two fence region when tiling */
  1611. if (INTEL_INFO(dev)->gen == 3)
  1612. gtt_size = 1024*1024;
  1613. else
  1614. gtt_size = 512*1024;
  1615. while (gtt_size < size)
  1616. gtt_size <<= 1;
  1617. return gtt_size;
  1618. }
  1619. /**
  1620. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1621. * @obj: object to check
  1622. *
  1623. * Return the required GTT alignment for an object, taking into account
  1624. * potential fence register mapping.
  1625. */
  1626. uint32_t
  1627. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1628. int tiling_mode, bool fenced)
  1629. {
  1630. /*
  1631. * Minimum alignment is 4k (GTT page size), but might be greater
  1632. * if a fence register is needed for the object.
  1633. */
  1634. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1635. tiling_mode == I915_TILING_NONE)
  1636. return 4096;
  1637. /*
  1638. * Previous chips need to be aligned to the size of the smallest
  1639. * fence register that can contain the object.
  1640. */
  1641. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1642. }
  1643. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1644. {
  1645. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1646. int ret;
  1647. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1648. return 0;
  1649. dev_priv->mm.shrinker_no_lock_stealing = true;
  1650. ret = drm_gem_create_mmap_offset(&obj->base);
  1651. if (ret != -ENOSPC)
  1652. goto out;
  1653. /* Badly fragmented mmap space? The only way we can recover
  1654. * space is by destroying unwanted objects. We can't randomly release
  1655. * mmap_offsets as userspace expects them to be persistent for the
  1656. * lifetime of the objects. The closest we can is to release the
  1657. * offsets on purgeable objects by truncating it and marking it purged,
  1658. * which prevents userspace from ever using that object again.
  1659. */
  1660. i915_gem_shrink(dev_priv,
  1661. obj->base.size >> PAGE_SHIFT,
  1662. I915_SHRINK_BOUND |
  1663. I915_SHRINK_UNBOUND |
  1664. I915_SHRINK_PURGEABLE);
  1665. ret = drm_gem_create_mmap_offset(&obj->base);
  1666. if (ret != -ENOSPC)
  1667. goto out;
  1668. i915_gem_shrink_all(dev_priv);
  1669. ret = drm_gem_create_mmap_offset(&obj->base);
  1670. out:
  1671. dev_priv->mm.shrinker_no_lock_stealing = false;
  1672. return ret;
  1673. }
  1674. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1675. {
  1676. drm_gem_free_mmap_offset(&obj->base);
  1677. }
  1678. int
  1679. i915_gem_mmap_gtt(struct drm_file *file,
  1680. struct drm_device *dev,
  1681. uint32_t handle,
  1682. uint64_t *offset)
  1683. {
  1684. struct drm_i915_gem_object *obj;
  1685. int ret;
  1686. ret = i915_mutex_lock_interruptible(dev);
  1687. if (ret)
  1688. return ret;
  1689. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1690. if (&obj->base == NULL) {
  1691. ret = -ENOENT;
  1692. goto unlock;
  1693. }
  1694. if (obj->madv != I915_MADV_WILLNEED) {
  1695. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1696. ret = -EFAULT;
  1697. goto out;
  1698. }
  1699. ret = i915_gem_object_create_mmap_offset(obj);
  1700. if (ret)
  1701. goto out;
  1702. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1703. out:
  1704. drm_gem_object_unreference(&obj->base);
  1705. unlock:
  1706. mutex_unlock(&dev->struct_mutex);
  1707. return ret;
  1708. }
  1709. /**
  1710. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1711. * @dev: DRM device
  1712. * @data: GTT mapping ioctl data
  1713. * @file: GEM object info
  1714. *
  1715. * Simply returns the fake offset to userspace so it can mmap it.
  1716. * The mmap call will end up in drm_gem_mmap(), which will set things
  1717. * up so we can get faults in the handler above.
  1718. *
  1719. * The fault handler will take care of binding the object into the GTT
  1720. * (since it may have been evicted to make room for something), allocating
  1721. * a fence register, and mapping the appropriate aperture address into
  1722. * userspace.
  1723. */
  1724. int
  1725. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1726. struct drm_file *file)
  1727. {
  1728. struct drm_i915_gem_mmap_gtt *args = data;
  1729. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1730. }
  1731. /* Immediately discard the backing storage */
  1732. static void
  1733. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1734. {
  1735. i915_gem_object_free_mmap_offset(obj);
  1736. if (obj->base.filp == NULL)
  1737. return;
  1738. /* Our goal here is to return as much of the memory as
  1739. * is possible back to the system as we are called from OOM.
  1740. * To do this we must instruct the shmfs to drop all of its
  1741. * backing pages, *now*.
  1742. */
  1743. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1744. obj->madv = __I915_MADV_PURGED;
  1745. }
  1746. /* Try to discard unwanted pages */
  1747. static void
  1748. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1749. {
  1750. struct address_space *mapping;
  1751. switch (obj->madv) {
  1752. case I915_MADV_DONTNEED:
  1753. i915_gem_object_truncate(obj);
  1754. case __I915_MADV_PURGED:
  1755. return;
  1756. }
  1757. if (obj->base.filp == NULL)
  1758. return;
  1759. mapping = file_inode(obj->base.filp)->i_mapping,
  1760. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1761. }
  1762. static void
  1763. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1764. {
  1765. struct sg_page_iter sg_iter;
  1766. int ret;
  1767. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1768. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1769. if (ret) {
  1770. /* In the event of a disaster, abandon all caches and
  1771. * hope for the best.
  1772. */
  1773. WARN_ON(ret != -EIO);
  1774. i915_gem_clflush_object(obj, true);
  1775. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1776. }
  1777. i915_gem_gtt_finish_object(obj);
  1778. if (i915_gem_object_needs_bit17_swizzle(obj))
  1779. i915_gem_object_save_bit_17_swizzle(obj);
  1780. if (obj->madv == I915_MADV_DONTNEED)
  1781. obj->dirty = 0;
  1782. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1783. struct page *page = sg_page_iter_page(&sg_iter);
  1784. if (obj->dirty)
  1785. set_page_dirty(page);
  1786. if (obj->madv == I915_MADV_WILLNEED)
  1787. mark_page_accessed(page);
  1788. page_cache_release(page);
  1789. }
  1790. obj->dirty = 0;
  1791. sg_free_table(obj->pages);
  1792. kfree(obj->pages);
  1793. }
  1794. int
  1795. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1796. {
  1797. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1798. if (obj->pages == NULL)
  1799. return 0;
  1800. if (obj->pages_pin_count)
  1801. return -EBUSY;
  1802. BUG_ON(i915_gem_obj_bound_any(obj));
  1803. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1804. * array, hence protect them from being reaped by removing them from gtt
  1805. * lists early. */
  1806. list_del(&obj->global_list);
  1807. ops->put_pages(obj);
  1808. obj->pages = NULL;
  1809. i915_gem_object_invalidate(obj);
  1810. return 0;
  1811. }
  1812. static int
  1813. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1814. {
  1815. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1816. int page_count, i;
  1817. struct address_space *mapping;
  1818. struct sg_table *st;
  1819. struct scatterlist *sg;
  1820. struct sg_page_iter sg_iter;
  1821. struct page *page;
  1822. unsigned long last_pfn = 0; /* suppress gcc warning */
  1823. int ret;
  1824. gfp_t gfp;
  1825. /* Assert that the object is not currently in any GPU domain. As it
  1826. * wasn't in the GTT, there shouldn't be any way it could have been in
  1827. * a GPU cache
  1828. */
  1829. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1830. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1831. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1832. if (st == NULL)
  1833. return -ENOMEM;
  1834. page_count = obj->base.size / PAGE_SIZE;
  1835. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1836. kfree(st);
  1837. return -ENOMEM;
  1838. }
  1839. /* Get the list of pages out of our struct file. They'll be pinned
  1840. * at this point until we release them.
  1841. *
  1842. * Fail silently without starting the shrinker
  1843. */
  1844. mapping = file_inode(obj->base.filp)->i_mapping;
  1845. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1846. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1847. sg = st->sgl;
  1848. st->nents = 0;
  1849. for (i = 0; i < page_count; i++) {
  1850. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1851. if (IS_ERR(page)) {
  1852. i915_gem_shrink(dev_priv,
  1853. page_count,
  1854. I915_SHRINK_BOUND |
  1855. I915_SHRINK_UNBOUND |
  1856. I915_SHRINK_PURGEABLE);
  1857. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1858. }
  1859. if (IS_ERR(page)) {
  1860. /* We've tried hard to allocate the memory by reaping
  1861. * our own buffer, now let the real VM do its job and
  1862. * go down in flames if truly OOM.
  1863. */
  1864. i915_gem_shrink_all(dev_priv);
  1865. page = shmem_read_mapping_page(mapping, i);
  1866. if (IS_ERR(page)) {
  1867. ret = PTR_ERR(page);
  1868. goto err_pages;
  1869. }
  1870. }
  1871. #ifdef CONFIG_SWIOTLB
  1872. if (swiotlb_nr_tbl()) {
  1873. st->nents++;
  1874. sg_set_page(sg, page, PAGE_SIZE, 0);
  1875. sg = sg_next(sg);
  1876. continue;
  1877. }
  1878. #endif
  1879. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1880. if (i)
  1881. sg = sg_next(sg);
  1882. st->nents++;
  1883. sg_set_page(sg, page, PAGE_SIZE, 0);
  1884. } else {
  1885. sg->length += PAGE_SIZE;
  1886. }
  1887. last_pfn = page_to_pfn(page);
  1888. /* Check that the i965g/gm workaround works. */
  1889. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1890. }
  1891. #ifdef CONFIG_SWIOTLB
  1892. if (!swiotlb_nr_tbl())
  1893. #endif
  1894. sg_mark_end(sg);
  1895. obj->pages = st;
  1896. ret = i915_gem_gtt_prepare_object(obj);
  1897. if (ret)
  1898. goto err_pages;
  1899. if (i915_gem_object_needs_bit17_swizzle(obj))
  1900. i915_gem_object_do_bit_17_swizzle(obj);
  1901. if (obj->tiling_mode != I915_TILING_NONE &&
  1902. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1903. i915_gem_object_pin_pages(obj);
  1904. return 0;
  1905. err_pages:
  1906. sg_mark_end(sg);
  1907. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1908. page_cache_release(sg_page_iter_page(&sg_iter));
  1909. sg_free_table(st);
  1910. kfree(st);
  1911. /* shmemfs first checks if there is enough memory to allocate the page
  1912. * and reports ENOSPC should there be insufficient, along with the usual
  1913. * ENOMEM for a genuine allocation failure.
  1914. *
  1915. * We use ENOSPC in our driver to mean that we have run out of aperture
  1916. * space and so want to translate the error from shmemfs back to our
  1917. * usual understanding of ENOMEM.
  1918. */
  1919. if (ret == -ENOSPC)
  1920. ret = -ENOMEM;
  1921. return ret;
  1922. }
  1923. /* Ensure that the associated pages are gathered from the backing storage
  1924. * and pinned into our object. i915_gem_object_get_pages() may be called
  1925. * multiple times before they are released by a single call to
  1926. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1927. * either as a result of memory pressure (reaping pages under the shrinker)
  1928. * or as the object is itself released.
  1929. */
  1930. int
  1931. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1932. {
  1933. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1934. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1935. int ret;
  1936. if (obj->pages)
  1937. return 0;
  1938. if (obj->madv != I915_MADV_WILLNEED) {
  1939. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1940. return -EFAULT;
  1941. }
  1942. BUG_ON(obj->pages_pin_count);
  1943. ret = ops->get_pages(obj);
  1944. if (ret)
  1945. return ret;
  1946. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1947. obj->get_page.sg = obj->pages->sgl;
  1948. obj->get_page.last = 0;
  1949. return 0;
  1950. }
  1951. void i915_vma_move_to_active(struct i915_vma *vma,
  1952. struct drm_i915_gem_request *req)
  1953. {
  1954. struct drm_i915_gem_object *obj = vma->obj;
  1955. struct intel_engine_cs *ring;
  1956. ring = i915_gem_request_get_ring(req);
  1957. /* Add a reference if we're newly entering the active list. */
  1958. if (obj->active == 0)
  1959. drm_gem_object_reference(&obj->base);
  1960. obj->active |= intel_ring_flag(ring);
  1961. list_move_tail(&obj->ring_list[ring->id], &ring->active_list);
  1962. i915_gem_request_assign(&obj->last_read_req[ring->id], req);
  1963. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  1964. }
  1965. static void
  1966. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  1967. {
  1968. RQ_BUG_ON(obj->last_write_req == NULL);
  1969. RQ_BUG_ON(!(obj->active & intel_ring_flag(obj->last_write_req->ring)));
  1970. i915_gem_request_assign(&obj->last_write_req, NULL);
  1971. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  1972. }
  1973. static void
  1974. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  1975. {
  1976. struct i915_vma *vma;
  1977. RQ_BUG_ON(obj->last_read_req[ring] == NULL);
  1978. RQ_BUG_ON(!(obj->active & (1 << ring)));
  1979. list_del_init(&obj->ring_list[ring]);
  1980. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  1981. if (obj->last_write_req && obj->last_write_req->ring->id == ring)
  1982. i915_gem_object_retire__write(obj);
  1983. obj->active &= ~(1 << ring);
  1984. if (obj->active)
  1985. return;
  1986. /* Bump our place on the bound list to keep it roughly in LRU order
  1987. * so that we don't steal from recently used but inactive objects
  1988. * (unless we are forced to ofc!)
  1989. */
  1990. list_move_tail(&obj->global_list,
  1991. &to_i915(obj->base.dev)->mm.bound_list);
  1992. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  1993. if (!list_empty(&vma->mm_list))
  1994. list_move_tail(&vma->mm_list, &vma->vm->inactive_list);
  1995. }
  1996. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  1997. drm_gem_object_unreference(&obj->base);
  1998. }
  1999. static int
  2000. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  2001. {
  2002. struct drm_i915_private *dev_priv = dev->dev_private;
  2003. struct intel_engine_cs *ring;
  2004. int ret, i, j;
  2005. /* Carefully retire all requests without writing to the rings */
  2006. for_each_ring(ring, dev_priv, i) {
  2007. ret = intel_ring_idle(ring);
  2008. if (ret)
  2009. return ret;
  2010. }
  2011. i915_gem_retire_requests(dev);
  2012. /* Finally reset hw state */
  2013. for_each_ring(ring, dev_priv, i) {
  2014. intel_ring_init_seqno(ring, seqno);
  2015. for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  2016. ring->semaphore.sync_seqno[j] = 0;
  2017. }
  2018. return 0;
  2019. }
  2020. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2021. {
  2022. struct drm_i915_private *dev_priv = dev->dev_private;
  2023. int ret;
  2024. if (seqno == 0)
  2025. return -EINVAL;
  2026. /* HWS page needs to be set less than what we
  2027. * will inject to ring
  2028. */
  2029. ret = i915_gem_init_seqno(dev, seqno - 1);
  2030. if (ret)
  2031. return ret;
  2032. /* Carefully set the last_seqno value so that wrap
  2033. * detection still works
  2034. */
  2035. dev_priv->next_seqno = seqno;
  2036. dev_priv->last_seqno = seqno - 1;
  2037. if (dev_priv->last_seqno == 0)
  2038. dev_priv->last_seqno--;
  2039. return 0;
  2040. }
  2041. int
  2042. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  2043. {
  2044. struct drm_i915_private *dev_priv = dev->dev_private;
  2045. /* reserve 0 for non-seqno */
  2046. if (dev_priv->next_seqno == 0) {
  2047. int ret = i915_gem_init_seqno(dev, 0);
  2048. if (ret)
  2049. return ret;
  2050. dev_priv->next_seqno = 1;
  2051. }
  2052. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2053. return 0;
  2054. }
  2055. /*
  2056. * NB: This function is not allowed to fail. Doing so would mean the the
  2057. * request is not being tracked for completion but the work itself is
  2058. * going to happen on the hardware. This would be a Bad Thing(tm).
  2059. */
  2060. void __i915_add_request(struct drm_i915_gem_request *request,
  2061. struct drm_i915_gem_object *obj,
  2062. bool flush_caches)
  2063. {
  2064. struct intel_engine_cs *ring;
  2065. struct drm_i915_private *dev_priv;
  2066. struct intel_ringbuffer *ringbuf;
  2067. u32 request_start;
  2068. int ret;
  2069. if (WARN_ON(request == NULL))
  2070. return;
  2071. ring = request->ring;
  2072. dev_priv = ring->dev->dev_private;
  2073. ringbuf = request->ringbuf;
  2074. /*
  2075. * To ensure that this call will not fail, space for its emissions
  2076. * should already have been reserved in the ring buffer. Let the ring
  2077. * know that it is time to use that space up.
  2078. */
  2079. intel_ring_reserved_space_use(ringbuf);
  2080. request_start = intel_ring_get_tail(ringbuf);
  2081. /*
  2082. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2083. * after having emitted the batchbuffer command. Hence we need to fix
  2084. * things up similar to emitting the lazy request. The difference here
  2085. * is that the flush _must_ happen before the next request, no matter
  2086. * what.
  2087. */
  2088. if (flush_caches) {
  2089. if (i915.enable_execlists)
  2090. ret = logical_ring_flush_all_caches(request);
  2091. else
  2092. ret = intel_ring_flush_all_caches(request);
  2093. /* Not allowed to fail! */
  2094. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2095. }
  2096. /* Record the position of the start of the request so that
  2097. * should we detect the updated seqno part-way through the
  2098. * GPU processing the request, we never over-estimate the
  2099. * position of the head.
  2100. */
  2101. request->postfix = intel_ring_get_tail(ringbuf);
  2102. if (i915.enable_execlists)
  2103. ret = ring->emit_request(request);
  2104. else {
  2105. ret = ring->add_request(request);
  2106. request->tail = intel_ring_get_tail(ringbuf);
  2107. }
  2108. /* Not allowed to fail! */
  2109. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2110. request->head = request_start;
  2111. /* Whilst this request exists, batch_obj will be on the
  2112. * active_list, and so will hold the active reference. Only when this
  2113. * request is retired will the the batch_obj be moved onto the
  2114. * inactive_list and lose its active reference. Hence we do not need
  2115. * to explicitly hold another reference here.
  2116. */
  2117. request->batch_obj = obj;
  2118. request->emitted_jiffies = jiffies;
  2119. ring->last_submitted_seqno = request->seqno;
  2120. list_add_tail(&request->list, &ring->request_list);
  2121. trace_i915_gem_request_add(request);
  2122. i915_queue_hangcheck(ring->dev);
  2123. queue_delayed_work(dev_priv->wq,
  2124. &dev_priv->mm.retire_work,
  2125. round_jiffies_up_relative(HZ));
  2126. intel_mark_busy(dev_priv->dev);
  2127. /* Sanity check that the reserved size was large enough. */
  2128. intel_ring_reserved_space_end(ringbuf);
  2129. }
  2130. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2131. const struct intel_context *ctx)
  2132. {
  2133. unsigned long elapsed;
  2134. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2135. if (ctx->hang_stats.banned)
  2136. return true;
  2137. if (ctx->hang_stats.ban_period_seconds &&
  2138. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2139. if (!i915_gem_context_is_default(ctx)) {
  2140. DRM_DEBUG("context hanging too fast, banning!\n");
  2141. return true;
  2142. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2143. if (i915_stop_ring_allow_warn(dev_priv))
  2144. DRM_ERROR("gpu hanging too fast, banning!\n");
  2145. return true;
  2146. }
  2147. }
  2148. return false;
  2149. }
  2150. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2151. struct intel_context *ctx,
  2152. const bool guilty)
  2153. {
  2154. struct i915_ctx_hang_stats *hs;
  2155. if (WARN_ON(!ctx))
  2156. return;
  2157. hs = &ctx->hang_stats;
  2158. if (guilty) {
  2159. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2160. hs->batch_active++;
  2161. hs->guilty_ts = get_seconds();
  2162. } else {
  2163. hs->batch_pending++;
  2164. }
  2165. }
  2166. void i915_gem_request_free(struct kref *req_ref)
  2167. {
  2168. struct drm_i915_gem_request *req = container_of(req_ref,
  2169. typeof(*req), ref);
  2170. struct intel_context *ctx = req->ctx;
  2171. if (req->file_priv)
  2172. i915_gem_request_remove_from_client(req);
  2173. if (ctx) {
  2174. if (i915.enable_execlists) {
  2175. if (ctx != req->ring->default_context)
  2176. intel_lr_context_unpin(req);
  2177. }
  2178. i915_gem_context_unreference(ctx);
  2179. }
  2180. kmem_cache_free(req->i915->requests, req);
  2181. }
  2182. int i915_gem_request_alloc(struct intel_engine_cs *ring,
  2183. struct intel_context *ctx,
  2184. struct drm_i915_gem_request **req_out)
  2185. {
  2186. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  2187. struct drm_i915_gem_request *req;
  2188. int ret;
  2189. if (!req_out)
  2190. return -EINVAL;
  2191. *req_out = NULL;
  2192. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2193. if (req == NULL)
  2194. return -ENOMEM;
  2195. ret = i915_gem_get_seqno(ring->dev, &req->seqno);
  2196. if (ret)
  2197. goto err;
  2198. kref_init(&req->ref);
  2199. req->i915 = dev_priv;
  2200. req->ring = ring;
  2201. req->ctx = ctx;
  2202. i915_gem_context_reference(req->ctx);
  2203. if (i915.enable_execlists)
  2204. ret = intel_logical_ring_alloc_request_extras(req);
  2205. else
  2206. ret = intel_ring_alloc_request_extras(req);
  2207. if (ret) {
  2208. i915_gem_context_unreference(req->ctx);
  2209. goto err;
  2210. }
  2211. /*
  2212. * Reserve space in the ring buffer for all the commands required to
  2213. * eventually emit this request. This is to guarantee that the
  2214. * i915_add_request() call can't fail. Note that the reserve may need
  2215. * to be redone if the request is not actually submitted straight
  2216. * away, e.g. because a GPU scheduler has deferred it.
  2217. */
  2218. if (i915.enable_execlists)
  2219. ret = intel_logical_ring_reserve_space(req);
  2220. else
  2221. ret = intel_ring_reserve_space(req);
  2222. if (ret) {
  2223. /*
  2224. * At this point, the request is fully allocated even if not
  2225. * fully prepared. Thus it can be cleaned up using the proper
  2226. * free code.
  2227. */
  2228. i915_gem_request_cancel(req);
  2229. return ret;
  2230. }
  2231. *req_out = req;
  2232. return 0;
  2233. err:
  2234. kmem_cache_free(dev_priv->requests, req);
  2235. return ret;
  2236. }
  2237. void i915_gem_request_cancel(struct drm_i915_gem_request *req)
  2238. {
  2239. intel_ring_reserved_space_cancel(req->ringbuf);
  2240. i915_gem_request_unreference(req);
  2241. }
  2242. struct drm_i915_gem_request *
  2243. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2244. {
  2245. struct drm_i915_gem_request *request;
  2246. list_for_each_entry(request, &ring->request_list, list) {
  2247. if (i915_gem_request_completed(request, false))
  2248. continue;
  2249. return request;
  2250. }
  2251. return NULL;
  2252. }
  2253. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2254. struct intel_engine_cs *ring)
  2255. {
  2256. struct drm_i915_gem_request *request;
  2257. bool ring_hung;
  2258. request = i915_gem_find_active_request(ring);
  2259. if (request == NULL)
  2260. return;
  2261. ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2262. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2263. list_for_each_entry_continue(request, &ring->request_list, list)
  2264. i915_set_reset_status(dev_priv, request->ctx, false);
  2265. }
  2266. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2267. struct intel_engine_cs *ring)
  2268. {
  2269. while (!list_empty(&ring->active_list)) {
  2270. struct drm_i915_gem_object *obj;
  2271. obj = list_first_entry(&ring->active_list,
  2272. struct drm_i915_gem_object,
  2273. ring_list[ring->id]);
  2274. i915_gem_object_retire__read(obj, ring->id);
  2275. }
  2276. /*
  2277. * Clear the execlists queue up before freeing the requests, as those
  2278. * are the ones that keep the context and ringbuffer backing objects
  2279. * pinned in place.
  2280. */
  2281. while (!list_empty(&ring->execlist_queue)) {
  2282. struct drm_i915_gem_request *submit_req;
  2283. submit_req = list_first_entry(&ring->execlist_queue,
  2284. struct drm_i915_gem_request,
  2285. execlist_link);
  2286. list_del(&submit_req->execlist_link);
  2287. if (submit_req->ctx != ring->default_context)
  2288. intel_lr_context_unpin(submit_req);
  2289. i915_gem_request_unreference(submit_req);
  2290. }
  2291. /*
  2292. * We must free the requests after all the corresponding objects have
  2293. * been moved off active lists. Which is the same order as the normal
  2294. * retire_requests function does. This is important if object hold
  2295. * implicit references on things like e.g. ppgtt address spaces through
  2296. * the request.
  2297. */
  2298. while (!list_empty(&ring->request_list)) {
  2299. struct drm_i915_gem_request *request;
  2300. request = list_first_entry(&ring->request_list,
  2301. struct drm_i915_gem_request,
  2302. list);
  2303. i915_gem_request_retire(request);
  2304. }
  2305. }
  2306. void i915_gem_reset(struct drm_device *dev)
  2307. {
  2308. struct drm_i915_private *dev_priv = dev->dev_private;
  2309. struct intel_engine_cs *ring;
  2310. int i;
  2311. /*
  2312. * Before we free the objects from the requests, we need to inspect
  2313. * them for finding the guilty party. As the requests only borrow
  2314. * their reference to the objects, the inspection must be done first.
  2315. */
  2316. for_each_ring(ring, dev_priv, i)
  2317. i915_gem_reset_ring_status(dev_priv, ring);
  2318. for_each_ring(ring, dev_priv, i)
  2319. i915_gem_reset_ring_cleanup(dev_priv, ring);
  2320. i915_gem_context_reset(dev);
  2321. i915_gem_restore_fences(dev);
  2322. WARN_ON(i915_verify_lists(dev));
  2323. }
  2324. /**
  2325. * This function clears the request list as sequence numbers are passed.
  2326. */
  2327. void
  2328. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2329. {
  2330. WARN_ON(i915_verify_lists(ring->dev));
  2331. /* Retire requests first as we use it above for the early return.
  2332. * If we retire requests last, we may use a later seqno and so clear
  2333. * the requests lists without clearing the active list, leading to
  2334. * confusion.
  2335. */
  2336. while (!list_empty(&ring->request_list)) {
  2337. struct drm_i915_gem_request *request;
  2338. request = list_first_entry(&ring->request_list,
  2339. struct drm_i915_gem_request,
  2340. list);
  2341. if (!i915_gem_request_completed(request, true))
  2342. break;
  2343. i915_gem_request_retire(request);
  2344. }
  2345. /* Move any buffers on the active list that are no longer referenced
  2346. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2347. * before we free the context associated with the requests.
  2348. */
  2349. while (!list_empty(&ring->active_list)) {
  2350. struct drm_i915_gem_object *obj;
  2351. obj = list_first_entry(&ring->active_list,
  2352. struct drm_i915_gem_object,
  2353. ring_list[ring->id]);
  2354. if (!list_empty(&obj->last_read_req[ring->id]->list))
  2355. break;
  2356. i915_gem_object_retire__read(obj, ring->id);
  2357. }
  2358. if (unlikely(ring->trace_irq_req &&
  2359. i915_gem_request_completed(ring->trace_irq_req, true))) {
  2360. ring->irq_put(ring);
  2361. i915_gem_request_assign(&ring->trace_irq_req, NULL);
  2362. }
  2363. WARN_ON(i915_verify_lists(ring->dev));
  2364. }
  2365. bool
  2366. i915_gem_retire_requests(struct drm_device *dev)
  2367. {
  2368. struct drm_i915_private *dev_priv = dev->dev_private;
  2369. struct intel_engine_cs *ring;
  2370. bool idle = true;
  2371. int i;
  2372. for_each_ring(ring, dev_priv, i) {
  2373. i915_gem_retire_requests_ring(ring);
  2374. idle &= list_empty(&ring->request_list);
  2375. if (i915.enable_execlists) {
  2376. unsigned long flags;
  2377. spin_lock_irqsave(&ring->execlist_lock, flags);
  2378. idle &= list_empty(&ring->execlist_queue);
  2379. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  2380. intel_execlists_retire_requests(ring);
  2381. }
  2382. }
  2383. if (idle)
  2384. mod_delayed_work(dev_priv->wq,
  2385. &dev_priv->mm.idle_work,
  2386. msecs_to_jiffies(100));
  2387. return idle;
  2388. }
  2389. static void
  2390. i915_gem_retire_work_handler(struct work_struct *work)
  2391. {
  2392. struct drm_i915_private *dev_priv =
  2393. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2394. struct drm_device *dev = dev_priv->dev;
  2395. bool idle;
  2396. /* Come back later if the device is busy... */
  2397. idle = false;
  2398. if (mutex_trylock(&dev->struct_mutex)) {
  2399. idle = i915_gem_retire_requests(dev);
  2400. mutex_unlock(&dev->struct_mutex);
  2401. }
  2402. if (!idle)
  2403. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2404. round_jiffies_up_relative(HZ));
  2405. }
  2406. static void
  2407. i915_gem_idle_work_handler(struct work_struct *work)
  2408. {
  2409. struct drm_i915_private *dev_priv =
  2410. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2411. struct drm_device *dev = dev_priv->dev;
  2412. struct intel_engine_cs *ring;
  2413. int i;
  2414. for_each_ring(ring, dev_priv, i)
  2415. if (!list_empty(&ring->request_list))
  2416. return;
  2417. intel_mark_idle(dev);
  2418. if (mutex_trylock(&dev->struct_mutex)) {
  2419. struct intel_engine_cs *ring;
  2420. int i;
  2421. for_each_ring(ring, dev_priv, i)
  2422. i915_gem_batch_pool_fini(&ring->batch_pool);
  2423. mutex_unlock(&dev->struct_mutex);
  2424. }
  2425. }
  2426. /**
  2427. * Ensures that an object will eventually get non-busy by flushing any required
  2428. * write domains, emitting any outstanding lazy request and retiring and
  2429. * completed requests.
  2430. */
  2431. static int
  2432. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2433. {
  2434. int i;
  2435. if (!obj->active)
  2436. return 0;
  2437. for (i = 0; i < I915_NUM_RINGS; i++) {
  2438. struct drm_i915_gem_request *req;
  2439. req = obj->last_read_req[i];
  2440. if (req == NULL)
  2441. continue;
  2442. if (list_empty(&req->list))
  2443. goto retire;
  2444. if (i915_gem_request_completed(req, true)) {
  2445. __i915_gem_request_retire__upto(req);
  2446. retire:
  2447. i915_gem_object_retire__read(obj, i);
  2448. }
  2449. }
  2450. return 0;
  2451. }
  2452. /**
  2453. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2454. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2455. *
  2456. * Returns 0 if successful, else an error is returned with the remaining time in
  2457. * the timeout parameter.
  2458. * -ETIME: object is still busy after timeout
  2459. * -ERESTARTSYS: signal interrupted the wait
  2460. * -ENONENT: object doesn't exist
  2461. * Also possible, but rare:
  2462. * -EAGAIN: GPU wedged
  2463. * -ENOMEM: damn
  2464. * -ENODEV: Internal IRQ fail
  2465. * -E?: The add request failed
  2466. *
  2467. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2468. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2469. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2470. * without holding struct_mutex the object may become re-busied before this
  2471. * function completes. A similar but shorter * race condition exists in the busy
  2472. * ioctl
  2473. */
  2474. int
  2475. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2476. {
  2477. struct drm_i915_private *dev_priv = dev->dev_private;
  2478. struct drm_i915_gem_wait *args = data;
  2479. struct drm_i915_gem_object *obj;
  2480. struct drm_i915_gem_request *req[I915_NUM_RINGS];
  2481. unsigned reset_counter;
  2482. int i, n = 0;
  2483. int ret;
  2484. if (args->flags != 0)
  2485. return -EINVAL;
  2486. ret = i915_mutex_lock_interruptible(dev);
  2487. if (ret)
  2488. return ret;
  2489. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2490. if (&obj->base == NULL) {
  2491. mutex_unlock(&dev->struct_mutex);
  2492. return -ENOENT;
  2493. }
  2494. /* Need to make sure the object gets inactive eventually. */
  2495. ret = i915_gem_object_flush_active(obj);
  2496. if (ret)
  2497. goto out;
  2498. if (!obj->active)
  2499. goto out;
  2500. /* Do this after OLR check to make sure we make forward progress polling
  2501. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2502. */
  2503. if (args->timeout_ns == 0) {
  2504. ret = -ETIME;
  2505. goto out;
  2506. }
  2507. drm_gem_object_unreference(&obj->base);
  2508. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2509. for (i = 0; i < I915_NUM_RINGS; i++) {
  2510. if (obj->last_read_req[i] == NULL)
  2511. continue;
  2512. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2513. }
  2514. mutex_unlock(&dev->struct_mutex);
  2515. for (i = 0; i < n; i++) {
  2516. if (ret == 0)
  2517. ret = __i915_wait_request(req[i], reset_counter, true,
  2518. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2519. file->driver_priv);
  2520. i915_gem_request_unreference__unlocked(req[i]);
  2521. }
  2522. return ret;
  2523. out:
  2524. drm_gem_object_unreference(&obj->base);
  2525. mutex_unlock(&dev->struct_mutex);
  2526. return ret;
  2527. }
  2528. static int
  2529. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2530. struct intel_engine_cs *to,
  2531. struct drm_i915_gem_request *from_req,
  2532. struct drm_i915_gem_request **to_req)
  2533. {
  2534. struct intel_engine_cs *from;
  2535. int ret;
  2536. from = i915_gem_request_get_ring(from_req);
  2537. if (to == from)
  2538. return 0;
  2539. if (i915_gem_request_completed(from_req, true))
  2540. return 0;
  2541. if (!i915_semaphore_is_enabled(obj->base.dev)) {
  2542. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2543. ret = __i915_wait_request(from_req,
  2544. atomic_read(&i915->gpu_error.reset_counter),
  2545. i915->mm.interruptible,
  2546. NULL,
  2547. &i915->rps.semaphores);
  2548. if (ret)
  2549. return ret;
  2550. i915_gem_object_retire_request(obj, from_req);
  2551. } else {
  2552. int idx = intel_ring_sync_index(from, to);
  2553. u32 seqno = i915_gem_request_get_seqno(from_req);
  2554. WARN_ON(!to_req);
  2555. if (seqno <= from->semaphore.sync_seqno[idx])
  2556. return 0;
  2557. if (*to_req == NULL) {
  2558. ret = i915_gem_request_alloc(to, to->default_context, to_req);
  2559. if (ret)
  2560. return ret;
  2561. }
  2562. trace_i915_gem_ring_sync_to(*to_req, from, from_req);
  2563. ret = to->semaphore.sync_to(*to_req, from, seqno);
  2564. if (ret)
  2565. return ret;
  2566. /* We use last_read_req because sync_to()
  2567. * might have just caused seqno wrap under
  2568. * the radar.
  2569. */
  2570. from->semaphore.sync_seqno[idx] =
  2571. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2572. }
  2573. return 0;
  2574. }
  2575. /**
  2576. * i915_gem_object_sync - sync an object to a ring.
  2577. *
  2578. * @obj: object which may be in use on another ring.
  2579. * @to: ring we wish to use the object on. May be NULL.
  2580. * @to_req: request we wish to use the object for. See below.
  2581. * This will be allocated and returned if a request is
  2582. * required but not passed in.
  2583. *
  2584. * This code is meant to abstract object synchronization with the GPU.
  2585. * Calling with NULL implies synchronizing the object with the CPU
  2586. * rather than a particular GPU ring. Conceptually we serialise writes
  2587. * between engines inside the GPU. We only allow one engine to write
  2588. * into a buffer at any time, but multiple readers. To ensure each has
  2589. * a coherent view of memory, we must:
  2590. *
  2591. * - If there is an outstanding write request to the object, the new
  2592. * request must wait for it to complete (either CPU or in hw, requests
  2593. * on the same ring will be naturally ordered).
  2594. *
  2595. * - If we are a write request (pending_write_domain is set), the new
  2596. * request must wait for outstanding read requests to complete.
  2597. *
  2598. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2599. * rings to_req must be non-NULL. However, a request does not have to be
  2600. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2601. * request will be allocated automatically and returned through *to_req. Note
  2602. * that it is not guaranteed that commands will be emitted (because the system
  2603. * might already be idle). Hence there is no need to create a request that
  2604. * might never have any work submitted. Note further that if a request is
  2605. * returned in *to_req, it is the responsibility of the caller to submit
  2606. * that request (after potentially adding more work to it).
  2607. *
  2608. * Returns 0 if successful, else propagates up the lower layer error.
  2609. */
  2610. int
  2611. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2612. struct intel_engine_cs *to,
  2613. struct drm_i915_gem_request **to_req)
  2614. {
  2615. const bool readonly = obj->base.pending_write_domain == 0;
  2616. struct drm_i915_gem_request *req[I915_NUM_RINGS];
  2617. int ret, i, n;
  2618. if (!obj->active)
  2619. return 0;
  2620. if (to == NULL)
  2621. return i915_gem_object_wait_rendering(obj, readonly);
  2622. n = 0;
  2623. if (readonly) {
  2624. if (obj->last_write_req)
  2625. req[n++] = obj->last_write_req;
  2626. } else {
  2627. for (i = 0; i < I915_NUM_RINGS; i++)
  2628. if (obj->last_read_req[i])
  2629. req[n++] = obj->last_read_req[i];
  2630. }
  2631. for (i = 0; i < n; i++) {
  2632. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2633. if (ret)
  2634. return ret;
  2635. }
  2636. return 0;
  2637. }
  2638. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2639. {
  2640. u32 old_write_domain, old_read_domains;
  2641. /* Force a pagefault for domain tracking on next user access */
  2642. i915_gem_release_mmap(obj);
  2643. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2644. return;
  2645. /* Wait for any direct GTT access to complete */
  2646. mb();
  2647. old_read_domains = obj->base.read_domains;
  2648. old_write_domain = obj->base.write_domain;
  2649. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2650. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2651. trace_i915_gem_object_change_domain(obj,
  2652. old_read_domains,
  2653. old_write_domain);
  2654. }
  2655. static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
  2656. {
  2657. struct drm_i915_gem_object *obj = vma->obj;
  2658. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2659. int ret;
  2660. if (list_empty(&vma->vma_link))
  2661. return 0;
  2662. if (!drm_mm_node_allocated(&vma->node)) {
  2663. i915_gem_vma_destroy(vma);
  2664. return 0;
  2665. }
  2666. if (vma->pin_count)
  2667. return -EBUSY;
  2668. BUG_ON(obj->pages == NULL);
  2669. if (wait) {
  2670. ret = i915_gem_object_wait_rendering(obj, false);
  2671. if (ret)
  2672. return ret;
  2673. }
  2674. if (i915_is_ggtt(vma->vm) &&
  2675. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2676. i915_gem_object_finish_gtt(obj);
  2677. /* release the fence reg _after_ flushing */
  2678. ret = i915_gem_object_put_fence(obj);
  2679. if (ret)
  2680. return ret;
  2681. }
  2682. trace_i915_vma_unbind(vma);
  2683. vma->vm->unbind_vma(vma);
  2684. vma->bound = 0;
  2685. list_del_init(&vma->mm_list);
  2686. if (i915_is_ggtt(vma->vm)) {
  2687. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2688. obj->map_and_fenceable = false;
  2689. } else if (vma->ggtt_view.pages) {
  2690. sg_free_table(vma->ggtt_view.pages);
  2691. kfree(vma->ggtt_view.pages);
  2692. }
  2693. vma->ggtt_view.pages = NULL;
  2694. }
  2695. drm_mm_remove_node(&vma->node);
  2696. i915_gem_vma_destroy(vma);
  2697. /* Since the unbound list is global, only move to that list if
  2698. * no more VMAs exist. */
  2699. if (list_empty(&obj->vma_list))
  2700. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2701. /* And finally now the object is completely decoupled from this vma,
  2702. * we can drop its hold on the backing storage and allow it to be
  2703. * reaped by the shrinker.
  2704. */
  2705. i915_gem_object_unpin_pages(obj);
  2706. return 0;
  2707. }
  2708. int i915_vma_unbind(struct i915_vma *vma)
  2709. {
  2710. return __i915_vma_unbind(vma, true);
  2711. }
  2712. int __i915_vma_unbind_no_wait(struct i915_vma *vma)
  2713. {
  2714. return __i915_vma_unbind(vma, false);
  2715. }
  2716. int i915_gpu_idle(struct drm_device *dev)
  2717. {
  2718. struct drm_i915_private *dev_priv = dev->dev_private;
  2719. struct intel_engine_cs *ring;
  2720. int ret, i;
  2721. /* Flush everything onto the inactive list. */
  2722. for_each_ring(ring, dev_priv, i) {
  2723. if (!i915.enable_execlists) {
  2724. struct drm_i915_gem_request *req;
  2725. ret = i915_gem_request_alloc(ring, ring->default_context, &req);
  2726. if (ret)
  2727. return ret;
  2728. ret = i915_switch_context(req);
  2729. if (ret) {
  2730. i915_gem_request_cancel(req);
  2731. return ret;
  2732. }
  2733. i915_add_request_no_flush(req);
  2734. }
  2735. ret = intel_ring_idle(ring);
  2736. if (ret)
  2737. return ret;
  2738. }
  2739. WARN_ON(i915_verify_lists(dev));
  2740. return 0;
  2741. }
  2742. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2743. unsigned long cache_level)
  2744. {
  2745. struct drm_mm_node *gtt_space = &vma->node;
  2746. struct drm_mm_node *other;
  2747. /*
  2748. * On some machines we have to be careful when putting differing types
  2749. * of snoopable memory together to avoid the prefetcher crossing memory
  2750. * domains and dying. During vm initialisation, we decide whether or not
  2751. * these constraints apply and set the drm_mm.color_adjust
  2752. * appropriately.
  2753. */
  2754. if (vma->vm->mm.color_adjust == NULL)
  2755. return true;
  2756. if (!drm_mm_node_allocated(gtt_space))
  2757. return true;
  2758. if (list_empty(&gtt_space->node_list))
  2759. return true;
  2760. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2761. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2762. return false;
  2763. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2764. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2765. return false;
  2766. return true;
  2767. }
  2768. /**
  2769. * Finds free space in the GTT aperture and binds the object or a view of it
  2770. * there.
  2771. */
  2772. static struct i915_vma *
  2773. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2774. struct i915_address_space *vm,
  2775. const struct i915_ggtt_view *ggtt_view,
  2776. unsigned alignment,
  2777. uint64_t flags)
  2778. {
  2779. struct drm_device *dev = obj->base.dev;
  2780. struct drm_i915_private *dev_priv = dev->dev_private;
  2781. u32 fence_alignment, unfenced_alignment;
  2782. u32 search_flag, alloc_flag;
  2783. u64 start, end;
  2784. u64 size, fence_size;
  2785. struct i915_vma *vma;
  2786. int ret;
  2787. if (i915_is_ggtt(vm)) {
  2788. u32 view_size;
  2789. if (WARN_ON(!ggtt_view))
  2790. return ERR_PTR(-EINVAL);
  2791. view_size = i915_ggtt_view_size(obj, ggtt_view);
  2792. fence_size = i915_gem_get_gtt_size(dev,
  2793. view_size,
  2794. obj->tiling_mode);
  2795. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2796. view_size,
  2797. obj->tiling_mode,
  2798. true);
  2799. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  2800. view_size,
  2801. obj->tiling_mode,
  2802. false);
  2803. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  2804. } else {
  2805. fence_size = i915_gem_get_gtt_size(dev,
  2806. obj->base.size,
  2807. obj->tiling_mode);
  2808. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2809. obj->base.size,
  2810. obj->tiling_mode,
  2811. true);
  2812. unfenced_alignment =
  2813. i915_gem_get_gtt_alignment(dev,
  2814. obj->base.size,
  2815. obj->tiling_mode,
  2816. false);
  2817. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2818. }
  2819. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2820. end = vm->total;
  2821. if (flags & PIN_MAPPABLE)
  2822. end = min_t(u64, end, dev_priv->gtt.mappable_end);
  2823. if (flags & PIN_ZONE_4G)
  2824. end = min_t(u64, end, (1ULL << 32));
  2825. if (alignment == 0)
  2826. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2827. unfenced_alignment;
  2828. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2829. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  2830. ggtt_view ? ggtt_view->type : 0,
  2831. alignment);
  2832. return ERR_PTR(-EINVAL);
  2833. }
  2834. /* If binding the object/GGTT view requires more space than the entire
  2835. * aperture has, reject it early before evicting everything in a vain
  2836. * attempt to find space.
  2837. */
  2838. if (size > end) {
  2839. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
  2840. ggtt_view ? ggtt_view->type : 0,
  2841. size,
  2842. flags & PIN_MAPPABLE ? "mappable" : "total",
  2843. end);
  2844. return ERR_PTR(-E2BIG);
  2845. }
  2846. ret = i915_gem_object_get_pages(obj);
  2847. if (ret)
  2848. return ERR_PTR(ret);
  2849. i915_gem_object_pin_pages(obj);
  2850. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  2851. i915_gem_obj_lookup_or_create_vma(obj, vm);
  2852. if (IS_ERR(vma))
  2853. goto err_unpin;
  2854. if (flags & PIN_HIGH) {
  2855. search_flag = DRM_MM_SEARCH_BELOW;
  2856. alloc_flag = DRM_MM_CREATE_TOP;
  2857. } else {
  2858. search_flag = DRM_MM_SEARCH_DEFAULT;
  2859. alloc_flag = DRM_MM_CREATE_DEFAULT;
  2860. }
  2861. search_free:
  2862. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2863. size, alignment,
  2864. obj->cache_level,
  2865. start, end,
  2866. search_flag,
  2867. alloc_flag);
  2868. if (ret) {
  2869. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2870. obj->cache_level,
  2871. start, end,
  2872. flags);
  2873. if (ret == 0)
  2874. goto search_free;
  2875. goto err_free_vma;
  2876. }
  2877. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  2878. ret = -EINVAL;
  2879. goto err_remove_node;
  2880. }
  2881. trace_i915_vma_bind(vma, flags);
  2882. ret = i915_vma_bind(vma, obj->cache_level, flags);
  2883. if (ret)
  2884. goto err_remove_node;
  2885. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2886. list_add_tail(&vma->mm_list, &vm->inactive_list);
  2887. return vma;
  2888. err_remove_node:
  2889. drm_mm_remove_node(&vma->node);
  2890. err_free_vma:
  2891. i915_gem_vma_destroy(vma);
  2892. vma = ERR_PTR(ret);
  2893. err_unpin:
  2894. i915_gem_object_unpin_pages(obj);
  2895. return vma;
  2896. }
  2897. bool
  2898. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2899. bool force)
  2900. {
  2901. /* If we don't have a page list set up, then we're not pinned
  2902. * to GPU, and we can ignore the cache flush because it'll happen
  2903. * again at bind time.
  2904. */
  2905. if (obj->pages == NULL)
  2906. return false;
  2907. /*
  2908. * Stolen memory is always coherent with the GPU as it is explicitly
  2909. * marked as wc by the system, or the system is cache-coherent.
  2910. */
  2911. if (obj->stolen || obj->phys_handle)
  2912. return false;
  2913. /* If the GPU is snooping the contents of the CPU cache,
  2914. * we do not need to manually clear the CPU cache lines. However,
  2915. * the caches are only snooped when the render cache is
  2916. * flushed/invalidated. As we always have to emit invalidations
  2917. * and flushes when moving into and out of the RENDER domain, correct
  2918. * snooping behaviour occurs naturally as the result of our domain
  2919. * tracking.
  2920. */
  2921. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  2922. obj->cache_dirty = true;
  2923. return false;
  2924. }
  2925. trace_i915_gem_object_clflush(obj);
  2926. drm_clflush_sg(obj->pages);
  2927. obj->cache_dirty = false;
  2928. return true;
  2929. }
  2930. /** Flushes the GTT write domain for the object if it's dirty. */
  2931. static void
  2932. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2933. {
  2934. uint32_t old_write_domain;
  2935. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2936. return;
  2937. /* No actual flushing is required for the GTT write domain. Writes
  2938. * to it immediately go to main memory as far as we know, so there's
  2939. * no chipset flush. It also doesn't land in render cache.
  2940. *
  2941. * However, we do have to enforce the order so that all writes through
  2942. * the GTT land before any writes to the device, such as updates to
  2943. * the GATT itself.
  2944. */
  2945. wmb();
  2946. old_write_domain = obj->base.write_domain;
  2947. obj->base.write_domain = 0;
  2948. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  2949. trace_i915_gem_object_change_domain(obj,
  2950. obj->base.read_domains,
  2951. old_write_domain);
  2952. }
  2953. /** Flushes the CPU write domain for the object if it's dirty. */
  2954. static void
  2955. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2956. {
  2957. uint32_t old_write_domain;
  2958. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2959. return;
  2960. if (i915_gem_clflush_object(obj, obj->pin_display))
  2961. i915_gem_chipset_flush(obj->base.dev);
  2962. old_write_domain = obj->base.write_domain;
  2963. obj->base.write_domain = 0;
  2964. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  2965. trace_i915_gem_object_change_domain(obj,
  2966. obj->base.read_domains,
  2967. old_write_domain);
  2968. }
  2969. /**
  2970. * Moves a single object to the GTT read, and possibly write domain.
  2971. *
  2972. * This function returns when the move is complete, including waiting on
  2973. * flushes to occur.
  2974. */
  2975. int
  2976. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2977. {
  2978. uint32_t old_write_domain, old_read_domains;
  2979. struct i915_vma *vma;
  2980. int ret;
  2981. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2982. return 0;
  2983. ret = i915_gem_object_wait_rendering(obj, !write);
  2984. if (ret)
  2985. return ret;
  2986. /* Flush and acquire obj->pages so that we are coherent through
  2987. * direct access in memory with previous cached writes through
  2988. * shmemfs and that our cache domain tracking remains valid.
  2989. * For example, if the obj->filp was moved to swap without us
  2990. * being notified and releasing the pages, we would mistakenly
  2991. * continue to assume that the obj remained out of the CPU cached
  2992. * domain.
  2993. */
  2994. ret = i915_gem_object_get_pages(obj);
  2995. if (ret)
  2996. return ret;
  2997. i915_gem_object_flush_cpu_write_domain(obj);
  2998. /* Serialise direct access to this object with the barriers for
  2999. * coherent writes from the GPU, by effectively invalidating the
  3000. * GTT domain upon first access.
  3001. */
  3002. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3003. mb();
  3004. old_write_domain = obj->base.write_domain;
  3005. old_read_domains = obj->base.read_domains;
  3006. /* It should now be out of any other write domains, and we can update
  3007. * the domain values for our changes.
  3008. */
  3009. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3010. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3011. if (write) {
  3012. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3013. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3014. obj->dirty = 1;
  3015. }
  3016. trace_i915_gem_object_change_domain(obj,
  3017. old_read_domains,
  3018. old_write_domain);
  3019. /* And bump the LRU for this access */
  3020. vma = i915_gem_obj_to_ggtt(obj);
  3021. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3022. list_move_tail(&vma->mm_list,
  3023. &to_i915(obj->base.dev)->gtt.base.inactive_list);
  3024. return 0;
  3025. }
  3026. /**
  3027. * Changes the cache-level of an object across all VMA.
  3028. *
  3029. * After this function returns, the object will be in the new cache-level
  3030. * across all GTT and the contents of the backing storage will be coherent,
  3031. * with respect to the new cache-level. In order to keep the backing storage
  3032. * coherent for all users, we only allow a single cache level to be set
  3033. * globally on the object and prevent it from being changed whilst the
  3034. * hardware is reading from the object. That is if the object is currently
  3035. * on the scanout it will be set to uncached (or equivalent display
  3036. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3037. * that all direct access to the scanout remains coherent.
  3038. */
  3039. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3040. enum i915_cache_level cache_level)
  3041. {
  3042. struct drm_device *dev = obj->base.dev;
  3043. struct i915_vma *vma, *next;
  3044. bool bound = false;
  3045. int ret = 0;
  3046. if (obj->cache_level == cache_level)
  3047. goto out;
  3048. /* Inspect the list of currently bound VMA and unbind any that would
  3049. * be invalid given the new cache-level. This is principally to
  3050. * catch the issue of the CS prefetch crossing page boundaries and
  3051. * reading an invalid PTE on older architectures.
  3052. */
  3053. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3054. if (!drm_mm_node_allocated(&vma->node))
  3055. continue;
  3056. if (vma->pin_count) {
  3057. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3058. return -EBUSY;
  3059. }
  3060. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3061. ret = i915_vma_unbind(vma);
  3062. if (ret)
  3063. return ret;
  3064. } else
  3065. bound = true;
  3066. }
  3067. /* We can reuse the existing drm_mm nodes but need to change the
  3068. * cache-level on the PTE. We could simply unbind them all and
  3069. * rebind with the correct cache-level on next use. However since
  3070. * we already have a valid slot, dma mapping, pages etc, we may as
  3071. * rewrite the PTE in the belief that doing so tramples upon less
  3072. * state and so involves less work.
  3073. */
  3074. if (bound) {
  3075. /* Before we change the PTE, the GPU must not be accessing it.
  3076. * If we wait upon the object, we know that all the bound
  3077. * VMA are no longer active.
  3078. */
  3079. ret = i915_gem_object_wait_rendering(obj, false);
  3080. if (ret)
  3081. return ret;
  3082. if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
  3083. /* Access to snoopable pages through the GTT is
  3084. * incoherent and on some machines causes a hard
  3085. * lockup. Relinquish the CPU mmaping to force
  3086. * userspace to refault in the pages and we can
  3087. * then double check if the GTT mapping is still
  3088. * valid for that pointer access.
  3089. */
  3090. i915_gem_release_mmap(obj);
  3091. /* As we no longer need a fence for GTT access,
  3092. * we can relinquish it now (and so prevent having
  3093. * to steal a fence from someone else on the next
  3094. * fence request). Note GPU activity would have
  3095. * dropped the fence as all snoopable access is
  3096. * supposed to be linear.
  3097. */
  3098. ret = i915_gem_object_put_fence(obj);
  3099. if (ret)
  3100. return ret;
  3101. } else {
  3102. /* We either have incoherent backing store and
  3103. * so no GTT access or the architecture is fully
  3104. * coherent. In such cases, existing GTT mmaps
  3105. * ignore the cache bit in the PTE and we can
  3106. * rewrite it without confusing the GPU or having
  3107. * to force userspace to fault back in its mmaps.
  3108. */
  3109. }
  3110. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  3111. if (!drm_mm_node_allocated(&vma->node))
  3112. continue;
  3113. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3114. if (ret)
  3115. return ret;
  3116. }
  3117. }
  3118. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3119. vma->node.color = cache_level;
  3120. obj->cache_level = cache_level;
  3121. out:
  3122. /* Flush the dirty CPU caches to the backing storage so that the
  3123. * object is now coherent at its new cache level (with respect
  3124. * to the access domain).
  3125. */
  3126. if (obj->cache_dirty &&
  3127. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  3128. cpu_write_needs_clflush(obj)) {
  3129. if (i915_gem_clflush_object(obj, true))
  3130. i915_gem_chipset_flush(obj->base.dev);
  3131. }
  3132. return 0;
  3133. }
  3134. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3135. struct drm_file *file)
  3136. {
  3137. struct drm_i915_gem_caching *args = data;
  3138. struct drm_i915_gem_object *obj;
  3139. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3140. if (&obj->base == NULL)
  3141. return -ENOENT;
  3142. switch (obj->cache_level) {
  3143. case I915_CACHE_LLC:
  3144. case I915_CACHE_L3_LLC:
  3145. args->caching = I915_CACHING_CACHED;
  3146. break;
  3147. case I915_CACHE_WT:
  3148. args->caching = I915_CACHING_DISPLAY;
  3149. break;
  3150. default:
  3151. args->caching = I915_CACHING_NONE;
  3152. break;
  3153. }
  3154. drm_gem_object_unreference_unlocked(&obj->base);
  3155. return 0;
  3156. }
  3157. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3158. struct drm_file *file)
  3159. {
  3160. struct drm_i915_private *dev_priv = dev->dev_private;
  3161. struct drm_i915_gem_caching *args = data;
  3162. struct drm_i915_gem_object *obj;
  3163. enum i915_cache_level level;
  3164. int ret;
  3165. switch (args->caching) {
  3166. case I915_CACHING_NONE:
  3167. level = I915_CACHE_NONE;
  3168. break;
  3169. case I915_CACHING_CACHED:
  3170. /*
  3171. * Due to a HW issue on BXT A stepping, GPU stores via a
  3172. * snooped mapping may leave stale data in a corresponding CPU
  3173. * cacheline, whereas normally such cachelines would get
  3174. * invalidated.
  3175. */
  3176. if (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)
  3177. return -ENODEV;
  3178. level = I915_CACHE_LLC;
  3179. break;
  3180. case I915_CACHING_DISPLAY:
  3181. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3182. break;
  3183. default:
  3184. return -EINVAL;
  3185. }
  3186. intel_runtime_pm_get(dev_priv);
  3187. ret = i915_mutex_lock_interruptible(dev);
  3188. if (ret)
  3189. goto rpm_put;
  3190. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3191. if (&obj->base == NULL) {
  3192. ret = -ENOENT;
  3193. goto unlock;
  3194. }
  3195. ret = i915_gem_object_set_cache_level(obj, level);
  3196. drm_gem_object_unreference(&obj->base);
  3197. unlock:
  3198. mutex_unlock(&dev->struct_mutex);
  3199. rpm_put:
  3200. intel_runtime_pm_put(dev_priv);
  3201. return ret;
  3202. }
  3203. /*
  3204. * Prepare buffer for display plane (scanout, cursors, etc).
  3205. * Can be called from an uninterruptible phase (modesetting) and allows
  3206. * any flushes to be pipelined (for pageflips).
  3207. */
  3208. int
  3209. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3210. u32 alignment,
  3211. struct intel_engine_cs *pipelined,
  3212. struct drm_i915_gem_request **pipelined_request,
  3213. const struct i915_ggtt_view *view)
  3214. {
  3215. u32 old_read_domains, old_write_domain;
  3216. int ret;
  3217. ret = i915_gem_object_sync(obj, pipelined, pipelined_request);
  3218. if (ret)
  3219. return ret;
  3220. /* Mark the pin_display early so that we account for the
  3221. * display coherency whilst setting up the cache domains.
  3222. */
  3223. obj->pin_display++;
  3224. /* The display engine is not coherent with the LLC cache on gen6. As
  3225. * a result, we make sure that the pinning that is about to occur is
  3226. * done with uncached PTEs. This is lowest common denominator for all
  3227. * chipsets.
  3228. *
  3229. * However for gen6+, we could do better by using the GFDT bit instead
  3230. * of uncaching, which would allow us to flush all the LLC-cached data
  3231. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3232. */
  3233. ret = i915_gem_object_set_cache_level(obj,
  3234. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3235. if (ret)
  3236. goto err_unpin_display;
  3237. /* As the user may map the buffer once pinned in the display plane
  3238. * (e.g. libkms for the bootup splash), we have to ensure that we
  3239. * always use map_and_fenceable for all scanout buffers.
  3240. */
  3241. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3242. view->type == I915_GGTT_VIEW_NORMAL ?
  3243. PIN_MAPPABLE : 0);
  3244. if (ret)
  3245. goto err_unpin_display;
  3246. i915_gem_object_flush_cpu_write_domain(obj);
  3247. old_write_domain = obj->base.write_domain;
  3248. old_read_domains = obj->base.read_domains;
  3249. /* It should now be out of any other write domains, and we can update
  3250. * the domain values for our changes.
  3251. */
  3252. obj->base.write_domain = 0;
  3253. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3254. trace_i915_gem_object_change_domain(obj,
  3255. old_read_domains,
  3256. old_write_domain);
  3257. return 0;
  3258. err_unpin_display:
  3259. obj->pin_display--;
  3260. return ret;
  3261. }
  3262. void
  3263. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3264. const struct i915_ggtt_view *view)
  3265. {
  3266. if (WARN_ON(obj->pin_display == 0))
  3267. return;
  3268. i915_gem_object_ggtt_unpin_view(obj, view);
  3269. obj->pin_display--;
  3270. }
  3271. /**
  3272. * Moves a single object to the CPU read, and possibly write domain.
  3273. *
  3274. * This function returns when the move is complete, including waiting on
  3275. * flushes to occur.
  3276. */
  3277. int
  3278. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3279. {
  3280. uint32_t old_write_domain, old_read_domains;
  3281. int ret;
  3282. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3283. return 0;
  3284. ret = i915_gem_object_wait_rendering(obj, !write);
  3285. if (ret)
  3286. return ret;
  3287. i915_gem_object_flush_gtt_write_domain(obj);
  3288. old_write_domain = obj->base.write_domain;
  3289. old_read_domains = obj->base.read_domains;
  3290. /* Flush the CPU cache if it's still invalid. */
  3291. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3292. i915_gem_clflush_object(obj, false);
  3293. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3294. }
  3295. /* It should now be out of any other write domains, and we can update
  3296. * the domain values for our changes.
  3297. */
  3298. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3299. /* If we're writing through the CPU, then the GPU read domains will
  3300. * need to be invalidated at next use.
  3301. */
  3302. if (write) {
  3303. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3304. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3305. }
  3306. trace_i915_gem_object_change_domain(obj,
  3307. old_read_domains,
  3308. old_write_domain);
  3309. return 0;
  3310. }
  3311. /* Throttle our rendering by waiting until the ring has completed our requests
  3312. * emitted over 20 msec ago.
  3313. *
  3314. * Note that if we were to use the current jiffies each time around the loop,
  3315. * we wouldn't escape the function with any frames outstanding if the time to
  3316. * render a frame was over 20ms.
  3317. *
  3318. * This should get us reasonable parallelism between CPU and GPU but also
  3319. * relatively low latency when blocking on a particular request to finish.
  3320. */
  3321. static int
  3322. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3323. {
  3324. struct drm_i915_private *dev_priv = dev->dev_private;
  3325. struct drm_i915_file_private *file_priv = file->driver_priv;
  3326. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3327. struct drm_i915_gem_request *request, *target = NULL;
  3328. unsigned reset_counter;
  3329. int ret;
  3330. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3331. if (ret)
  3332. return ret;
  3333. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3334. if (ret)
  3335. return ret;
  3336. spin_lock(&file_priv->mm.lock);
  3337. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3338. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3339. break;
  3340. /*
  3341. * Note that the request might not have been submitted yet.
  3342. * In which case emitted_jiffies will be zero.
  3343. */
  3344. if (!request->emitted_jiffies)
  3345. continue;
  3346. target = request;
  3347. }
  3348. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3349. if (target)
  3350. i915_gem_request_reference(target);
  3351. spin_unlock(&file_priv->mm.lock);
  3352. if (target == NULL)
  3353. return 0;
  3354. ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
  3355. if (ret == 0)
  3356. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3357. i915_gem_request_unreference__unlocked(target);
  3358. return ret;
  3359. }
  3360. static bool
  3361. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3362. {
  3363. struct drm_i915_gem_object *obj = vma->obj;
  3364. if (alignment &&
  3365. vma->node.start & (alignment - 1))
  3366. return true;
  3367. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3368. return true;
  3369. if (flags & PIN_OFFSET_BIAS &&
  3370. vma->node.start < (flags & PIN_OFFSET_MASK))
  3371. return true;
  3372. return false;
  3373. }
  3374. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3375. {
  3376. struct drm_i915_gem_object *obj = vma->obj;
  3377. bool mappable, fenceable;
  3378. u32 fence_size, fence_alignment;
  3379. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3380. obj->base.size,
  3381. obj->tiling_mode);
  3382. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3383. obj->base.size,
  3384. obj->tiling_mode,
  3385. true);
  3386. fenceable = (vma->node.size == fence_size &&
  3387. (vma->node.start & (fence_alignment - 1)) == 0);
  3388. mappable = (vma->node.start + fence_size <=
  3389. to_i915(obj->base.dev)->gtt.mappable_end);
  3390. obj->map_and_fenceable = mappable && fenceable;
  3391. }
  3392. static int
  3393. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3394. struct i915_address_space *vm,
  3395. const struct i915_ggtt_view *ggtt_view,
  3396. uint32_t alignment,
  3397. uint64_t flags)
  3398. {
  3399. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3400. struct i915_vma *vma;
  3401. unsigned bound;
  3402. int ret;
  3403. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3404. return -ENODEV;
  3405. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3406. return -EINVAL;
  3407. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3408. return -EINVAL;
  3409. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3410. return -EINVAL;
  3411. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3412. i915_gem_obj_to_vma(obj, vm);
  3413. if (IS_ERR(vma))
  3414. return PTR_ERR(vma);
  3415. if (vma) {
  3416. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3417. return -EBUSY;
  3418. if (i915_vma_misplaced(vma, alignment, flags)) {
  3419. WARN(vma->pin_count,
  3420. "bo is already pinned in %s with incorrect alignment:"
  3421. " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
  3422. " obj->map_and_fenceable=%d\n",
  3423. ggtt_view ? "ggtt" : "ppgtt",
  3424. upper_32_bits(vma->node.start),
  3425. lower_32_bits(vma->node.start),
  3426. alignment,
  3427. !!(flags & PIN_MAPPABLE),
  3428. obj->map_and_fenceable);
  3429. ret = i915_vma_unbind(vma);
  3430. if (ret)
  3431. return ret;
  3432. vma = NULL;
  3433. }
  3434. }
  3435. bound = vma ? vma->bound : 0;
  3436. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3437. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3438. flags);
  3439. if (IS_ERR(vma))
  3440. return PTR_ERR(vma);
  3441. } else {
  3442. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3443. if (ret)
  3444. return ret;
  3445. }
  3446. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3447. (bound ^ vma->bound) & GLOBAL_BIND) {
  3448. __i915_vma_set_map_and_fenceable(vma);
  3449. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3450. }
  3451. vma->pin_count++;
  3452. return 0;
  3453. }
  3454. int
  3455. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3456. struct i915_address_space *vm,
  3457. uint32_t alignment,
  3458. uint64_t flags)
  3459. {
  3460. return i915_gem_object_do_pin(obj, vm,
  3461. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3462. alignment, flags);
  3463. }
  3464. int
  3465. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3466. const struct i915_ggtt_view *view,
  3467. uint32_t alignment,
  3468. uint64_t flags)
  3469. {
  3470. if (WARN_ONCE(!view, "no view specified"))
  3471. return -EINVAL;
  3472. return i915_gem_object_do_pin(obj, i915_obj_to_ggtt(obj), view,
  3473. alignment, flags | PIN_GLOBAL);
  3474. }
  3475. void
  3476. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3477. const struct i915_ggtt_view *view)
  3478. {
  3479. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3480. BUG_ON(!vma);
  3481. WARN_ON(vma->pin_count == 0);
  3482. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3483. --vma->pin_count;
  3484. }
  3485. int
  3486. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3487. struct drm_file *file)
  3488. {
  3489. struct drm_i915_gem_busy *args = data;
  3490. struct drm_i915_gem_object *obj;
  3491. int ret;
  3492. ret = i915_mutex_lock_interruptible(dev);
  3493. if (ret)
  3494. return ret;
  3495. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3496. if (&obj->base == NULL) {
  3497. ret = -ENOENT;
  3498. goto unlock;
  3499. }
  3500. /* Count all active objects as busy, even if they are currently not used
  3501. * by the gpu. Users of this interface expect objects to eventually
  3502. * become non-busy without any further actions, therefore emit any
  3503. * necessary flushes here.
  3504. */
  3505. ret = i915_gem_object_flush_active(obj);
  3506. if (ret)
  3507. goto unref;
  3508. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3509. args->busy = obj->active << 16;
  3510. if (obj->last_write_req)
  3511. args->busy |= obj->last_write_req->ring->id;
  3512. unref:
  3513. drm_gem_object_unreference(&obj->base);
  3514. unlock:
  3515. mutex_unlock(&dev->struct_mutex);
  3516. return ret;
  3517. }
  3518. int
  3519. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3520. struct drm_file *file_priv)
  3521. {
  3522. return i915_gem_ring_throttle(dev, file_priv);
  3523. }
  3524. int
  3525. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3526. struct drm_file *file_priv)
  3527. {
  3528. struct drm_i915_private *dev_priv = dev->dev_private;
  3529. struct drm_i915_gem_madvise *args = data;
  3530. struct drm_i915_gem_object *obj;
  3531. int ret;
  3532. switch (args->madv) {
  3533. case I915_MADV_DONTNEED:
  3534. case I915_MADV_WILLNEED:
  3535. break;
  3536. default:
  3537. return -EINVAL;
  3538. }
  3539. ret = i915_mutex_lock_interruptible(dev);
  3540. if (ret)
  3541. return ret;
  3542. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3543. if (&obj->base == NULL) {
  3544. ret = -ENOENT;
  3545. goto unlock;
  3546. }
  3547. if (i915_gem_obj_is_pinned(obj)) {
  3548. ret = -EINVAL;
  3549. goto out;
  3550. }
  3551. if (obj->pages &&
  3552. obj->tiling_mode != I915_TILING_NONE &&
  3553. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3554. if (obj->madv == I915_MADV_WILLNEED)
  3555. i915_gem_object_unpin_pages(obj);
  3556. if (args->madv == I915_MADV_WILLNEED)
  3557. i915_gem_object_pin_pages(obj);
  3558. }
  3559. if (obj->madv != __I915_MADV_PURGED)
  3560. obj->madv = args->madv;
  3561. /* if the object is no longer attached, discard its backing storage */
  3562. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3563. i915_gem_object_truncate(obj);
  3564. args->retained = obj->madv != __I915_MADV_PURGED;
  3565. out:
  3566. drm_gem_object_unreference(&obj->base);
  3567. unlock:
  3568. mutex_unlock(&dev->struct_mutex);
  3569. return ret;
  3570. }
  3571. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3572. const struct drm_i915_gem_object_ops *ops)
  3573. {
  3574. int i;
  3575. INIT_LIST_HEAD(&obj->global_list);
  3576. for (i = 0; i < I915_NUM_RINGS; i++)
  3577. INIT_LIST_HEAD(&obj->ring_list[i]);
  3578. INIT_LIST_HEAD(&obj->obj_exec_link);
  3579. INIT_LIST_HEAD(&obj->vma_list);
  3580. INIT_LIST_HEAD(&obj->batch_pool_link);
  3581. obj->ops = ops;
  3582. obj->fence_reg = I915_FENCE_REG_NONE;
  3583. obj->madv = I915_MADV_WILLNEED;
  3584. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3585. }
  3586. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3587. .get_pages = i915_gem_object_get_pages_gtt,
  3588. .put_pages = i915_gem_object_put_pages_gtt,
  3589. };
  3590. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3591. size_t size)
  3592. {
  3593. struct drm_i915_gem_object *obj;
  3594. struct address_space *mapping;
  3595. gfp_t mask;
  3596. obj = i915_gem_object_alloc(dev);
  3597. if (obj == NULL)
  3598. return NULL;
  3599. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3600. i915_gem_object_free(obj);
  3601. return NULL;
  3602. }
  3603. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3604. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3605. /* 965gm cannot relocate objects above 4GiB. */
  3606. mask &= ~__GFP_HIGHMEM;
  3607. mask |= __GFP_DMA32;
  3608. }
  3609. mapping = file_inode(obj->base.filp)->i_mapping;
  3610. mapping_set_gfp_mask(mapping, mask);
  3611. i915_gem_object_init(obj, &i915_gem_object_ops);
  3612. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3613. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3614. if (HAS_LLC(dev)) {
  3615. /* On some devices, we can have the GPU use the LLC (the CPU
  3616. * cache) for about a 10% performance improvement
  3617. * compared to uncached. Graphics requests other than
  3618. * display scanout are coherent with the CPU in
  3619. * accessing this cache. This means in this mode we
  3620. * don't need to clflush on the CPU side, and on the
  3621. * GPU side we only need to flush internal caches to
  3622. * get data visible to the CPU.
  3623. *
  3624. * However, we maintain the display planes as UC, and so
  3625. * need to rebind when first used as such.
  3626. */
  3627. obj->cache_level = I915_CACHE_LLC;
  3628. } else
  3629. obj->cache_level = I915_CACHE_NONE;
  3630. trace_i915_gem_object_create(obj);
  3631. return obj;
  3632. }
  3633. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3634. {
  3635. /* If we are the last user of the backing storage (be it shmemfs
  3636. * pages or stolen etc), we know that the pages are going to be
  3637. * immediately released. In this case, we can then skip copying
  3638. * back the contents from the GPU.
  3639. */
  3640. if (obj->madv != I915_MADV_WILLNEED)
  3641. return false;
  3642. if (obj->base.filp == NULL)
  3643. return true;
  3644. /* At first glance, this looks racy, but then again so would be
  3645. * userspace racing mmap against close. However, the first external
  3646. * reference to the filp can only be obtained through the
  3647. * i915_gem_mmap_ioctl() which safeguards us against the user
  3648. * acquiring such a reference whilst we are in the middle of
  3649. * freeing the object.
  3650. */
  3651. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3652. }
  3653. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3654. {
  3655. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3656. struct drm_device *dev = obj->base.dev;
  3657. struct drm_i915_private *dev_priv = dev->dev_private;
  3658. struct i915_vma *vma, *next;
  3659. intel_runtime_pm_get(dev_priv);
  3660. trace_i915_gem_object_destroy(obj);
  3661. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3662. int ret;
  3663. vma->pin_count = 0;
  3664. ret = i915_vma_unbind(vma);
  3665. if (WARN_ON(ret == -ERESTARTSYS)) {
  3666. bool was_interruptible;
  3667. was_interruptible = dev_priv->mm.interruptible;
  3668. dev_priv->mm.interruptible = false;
  3669. WARN_ON(i915_vma_unbind(vma));
  3670. dev_priv->mm.interruptible = was_interruptible;
  3671. }
  3672. }
  3673. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3674. * before progressing. */
  3675. if (obj->stolen)
  3676. i915_gem_object_unpin_pages(obj);
  3677. WARN_ON(obj->frontbuffer_bits);
  3678. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3679. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3680. obj->tiling_mode != I915_TILING_NONE)
  3681. i915_gem_object_unpin_pages(obj);
  3682. if (WARN_ON(obj->pages_pin_count))
  3683. obj->pages_pin_count = 0;
  3684. if (discard_backing_storage(obj))
  3685. obj->madv = I915_MADV_DONTNEED;
  3686. i915_gem_object_put_pages(obj);
  3687. i915_gem_object_free_mmap_offset(obj);
  3688. BUG_ON(obj->pages);
  3689. if (obj->base.import_attach)
  3690. drm_prime_gem_destroy(&obj->base, NULL);
  3691. if (obj->ops->release)
  3692. obj->ops->release(obj);
  3693. drm_gem_object_release(&obj->base);
  3694. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3695. kfree(obj->bit_17);
  3696. i915_gem_object_free(obj);
  3697. intel_runtime_pm_put(dev_priv);
  3698. }
  3699. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3700. struct i915_address_space *vm)
  3701. {
  3702. struct i915_vma *vma;
  3703. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  3704. if (i915_is_ggtt(vma->vm) &&
  3705. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  3706. continue;
  3707. if (vma->vm == vm)
  3708. return vma;
  3709. }
  3710. return NULL;
  3711. }
  3712. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  3713. const struct i915_ggtt_view *view)
  3714. {
  3715. struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
  3716. struct i915_vma *vma;
  3717. if (WARN_ONCE(!view, "no view specified"))
  3718. return ERR_PTR(-EINVAL);
  3719. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3720. if (vma->vm == ggtt &&
  3721. i915_ggtt_view_equal(&vma->ggtt_view, view))
  3722. return vma;
  3723. return NULL;
  3724. }
  3725. void i915_gem_vma_destroy(struct i915_vma *vma)
  3726. {
  3727. struct i915_address_space *vm = NULL;
  3728. WARN_ON(vma->node.allocated);
  3729. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3730. if (!list_empty(&vma->exec_list))
  3731. return;
  3732. vm = vma->vm;
  3733. if (!i915_is_ggtt(vm))
  3734. i915_ppgtt_put(i915_vm_to_ppgtt(vm));
  3735. list_del(&vma->vma_link);
  3736. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  3737. }
  3738. static void
  3739. i915_gem_stop_ringbuffers(struct drm_device *dev)
  3740. {
  3741. struct drm_i915_private *dev_priv = dev->dev_private;
  3742. struct intel_engine_cs *ring;
  3743. int i;
  3744. for_each_ring(ring, dev_priv, i)
  3745. dev_priv->gt.stop_ring(ring);
  3746. }
  3747. int
  3748. i915_gem_suspend(struct drm_device *dev)
  3749. {
  3750. struct drm_i915_private *dev_priv = dev->dev_private;
  3751. int ret = 0;
  3752. mutex_lock(&dev->struct_mutex);
  3753. ret = i915_gpu_idle(dev);
  3754. if (ret)
  3755. goto err;
  3756. i915_gem_retire_requests(dev);
  3757. i915_gem_stop_ringbuffers(dev);
  3758. mutex_unlock(&dev->struct_mutex);
  3759. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3760. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3761. flush_delayed_work(&dev_priv->mm.idle_work);
  3762. /* Assert that we sucessfully flushed all the work and
  3763. * reset the GPU back to its idle, low power state.
  3764. */
  3765. WARN_ON(dev_priv->mm.busy);
  3766. return 0;
  3767. err:
  3768. mutex_unlock(&dev->struct_mutex);
  3769. return ret;
  3770. }
  3771. int i915_gem_l3_remap(struct drm_i915_gem_request *req, int slice)
  3772. {
  3773. struct intel_engine_cs *ring = req->ring;
  3774. struct drm_device *dev = ring->dev;
  3775. struct drm_i915_private *dev_priv = dev->dev_private;
  3776. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  3777. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  3778. int i, ret;
  3779. if (!HAS_L3_DPF(dev) || !remap_info)
  3780. return 0;
  3781. ret = intel_ring_begin(req, GEN7_L3LOG_SIZE / 4 * 3);
  3782. if (ret)
  3783. return ret;
  3784. /*
  3785. * Note: We do not worry about the concurrent register cacheline hang
  3786. * here because no other code should access these registers other than
  3787. * at initialization time.
  3788. */
  3789. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3790. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  3791. intel_ring_emit(ring, reg_base + i);
  3792. intel_ring_emit(ring, remap_info[i/4]);
  3793. }
  3794. intel_ring_advance(ring);
  3795. return ret;
  3796. }
  3797. void i915_gem_init_swizzling(struct drm_device *dev)
  3798. {
  3799. struct drm_i915_private *dev_priv = dev->dev_private;
  3800. if (INTEL_INFO(dev)->gen < 5 ||
  3801. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3802. return;
  3803. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3804. DISP_TILE_SURFACE_SWIZZLING);
  3805. if (IS_GEN5(dev))
  3806. return;
  3807. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3808. if (IS_GEN6(dev))
  3809. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3810. else if (IS_GEN7(dev))
  3811. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3812. else if (IS_GEN8(dev))
  3813. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3814. else
  3815. BUG();
  3816. }
  3817. static void init_unused_ring(struct drm_device *dev, u32 base)
  3818. {
  3819. struct drm_i915_private *dev_priv = dev->dev_private;
  3820. I915_WRITE(RING_CTL(base), 0);
  3821. I915_WRITE(RING_HEAD(base), 0);
  3822. I915_WRITE(RING_TAIL(base), 0);
  3823. I915_WRITE(RING_START(base), 0);
  3824. }
  3825. static void init_unused_rings(struct drm_device *dev)
  3826. {
  3827. if (IS_I830(dev)) {
  3828. init_unused_ring(dev, PRB1_BASE);
  3829. init_unused_ring(dev, SRB0_BASE);
  3830. init_unused_ring(dev, SRB1_BASE);
  3831. init_unused_ring(dev, SRB2_BASE);
  3832. init_unused_ring(dev, SRB3_BASE);
  3833. } else if (IS_GEN2(dev)) {
  3834. init_unused_ring(dev, SRB0_BASE);
  3835. init_unused_ring(dev, SRB1_BASE);
  3836. } else if (IS_GEN3(dev)) {
  3837. init_unused_ring(dev, PRB1_BASE);
  3838. init_unused_ring(dev, PRB2_BASE);
  3839. }
  3840. }
  3841. int i915_gem_init_rings(struct drm_device *dev)
  3842. {
  3843. struct drm_i915_private *dev_priv = dev->dev_private;
  3844. int ret;
  3845. ret = intel_init_render_ring_buffer(dev);
  3846. if (ret)
  3847. return ret;
  3848. if (HAS_BSD(dev)) {
  3849. ret = intel_init_bsd_ring_buffer(dev);
  3850. if (ret)
  3851. goto cleanup_render_ring;
  3852. }
  3853. if (HAS_BLT(dev)) {
  3854. ret = intel_init_blt_ring_buffer(dev);
  3855. if (ret)
  3856. goto cleanup_bsd_ring;
  3857. }
  3858. if (HAS_VEBOX(dev)) {
  3859. ret = intel_init_vebox_ring_buffer(dev);
  3860. if (ret)
  3861. goto cleanup_blt_ring;
  3862. }
  3863. if (HAS_BSD2(dev)) {
  3864. ret = intel_init_bsd2_ring_buffer(dev);
  3865. if (ret)
  3866. goto cleanup_vebox_ring;
  3867. }
  3868. return 0;
  3869. cleanup_vebox_ring:
  3870. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3871. cleanup_blt_ring:
  3872. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3873. cleanup_bsd_ring:
  3874. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3875. cleanup_render_ring:
  3876. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3877. return ret;
  3878. }
  3879. int
  3880. i915_gem_init_hw(struct drm_device *dev)
  3881. {
  3882. struct drm_i915_private *dev_priv = dev->dev_private;
  3883. struct intel_engine_cs *ring;
  3884. int ret, i, j;
  3885. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3886. return -EIO;
  3887. /* Double layer security blanket, see i915_gem_init() */
  3888. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3889. if (dev_priv->ellc_size)
  3890. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3891. if (IS_HASWELL(dev))
  3892. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3893. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3894. if (HAS_PCH_NOP(dev)) {
  3895. if (IS_IVYBRIDGE(dev)) {
  3896. u32 temp = I915_READ(GEN7_MSG_CTL);
  3897. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3898. I915_WRITE(GEN7_MSG_CTL, temp);
  3899. } else if (INTEL_INFO(dev)->gen >= 7) {
  3900. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3901. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3902. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3903. }
  3904. }
  3905. i915_gem_init_swizzling(dev);
  3906. /*
  3907. * At least 830 can leave some of the unused rings
  3908. * "active" (ie. head != tail) after resume which
  3909. * will prevent c3 entry. Makes sure all unused rings
  3910. * are totally idle.
  3911. */
  3912. init_unused_rings(dev);
  3913. BUG_ON(!dev_priv->ring[RCS].default_context);
  3914. ret = i915_ppgtt_init_hw(dev);
  3915. if (ret) {
  3916. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3917. goto out;
  3918. }
  3919. /* Need to do basic initialisation of all rings first: */
  3920. for_each_ring(ring, dev_priv, i) {
  3921. ret = ring->init_hw(ring);
  3922. if (ret)
  3923. goto out;
  3924. }
  3925. /* We can't enable contexts until all firmware is loaded */
  3926. if (HAS_GUC_UCODE(dev)) {
  3927. ret = intel_guc_ucode_load(dev);
  3928. if (ret) {
  3929. /*
  3930. * If we got an error and GuC submission is enabled, map
  3931. * the error to -EIO so the GPU will be declared wedged.
  3932. * OTOH, if we didn't intend to use the GuC anyway, just
  3933. * discard the error and carry on.
  3934. */
  3935. DRM_ERROR("Failed to initialize GuC, error %d%s\n", ret,
  3936. i915.enable_guc_submission ? "" :
  3937. " (ignored)");
  3938. ret = i915.enable_guc_submission ? -EIO : 0;
  3939. if (ret)
  3940. goto out;
  3941. }
  3942. }
  3943. /*
  3944. * Increment the next seqno by 0x100 so we have a visible break
  3945. * on re-initialisation
  3946. */
  3947. ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
  3948. if (ret)
  3949. goto out;
  3950. /* Now it is safe to go back round and do everything else: */
  3951. for_each_ring(ring, dev_priv, i) {
  3952. struct drm_i915_gem_request *req;
  3953. WARN_ON(!ring->default_context);
  3954. ret = i915_gem_request_alloc(ring, ring->default_context, &req);
  3955. if (ret) {
  3956. i915_gem_cleanup_ringbuffer(dev);
  3957. goto out;
  3958. }
  3959. if (ring->id == RCS) {
  3960. for (j = 0; j < NUM_L3_SLICES(dev); j++)
  3961. i915_gem_l3_remap(req, j);
  3962. }
  3963. ret = i915_ppgtt_init_ring(req);
  3964. if (ret && ret != -EIO) {
  3965. DRM_ERROR("PPGTT enable ring #%d failed %d\n", i, ret);
  3966. i915_gem_request_cancel(req);
  3967. i915_gem_cleanup_ringbuffer(dev);
  3968. goto out;
  3969. }
  3970. ret = i915_gem_context_enable(req);
  3971. if (ret && ret != -EIO) {
  3972. DRM_ERROR("Context enable ring #%d failed %d\n", i, ret);
  3973. i915_gem_request_cancel(req);
  3974. i915_gem_cleanup_ringbuffer(dev);
  3975. goto out;
  3976. }
  3977. i915_add_request_no_flush(req);
  3978. }
  3979. out:
  3980. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3981. return ret;
  3982. }
  3983. int i915_gem_init(struct drm_device *dev)
  3984. {
  3985. struct drm_i915_private *dev_priv = dev->dev_private;
  3986. int ret;
  3987. i915.enable_execlists = intel_sanitize_enable_execlists(dev,
  3988. i915.enable_execlists);
  3989. mutex_lock(&dev->struct_mutex);
  3990. if (IS_VALLEYVIEW(dev)) {
  3991. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3992. I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  3993. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  3994. VLV_GTLC_ALLOWWAKEACK), 10))
  3995. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3996. }
  3997. if (!i915.enable_execlists) {
  3998. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  3999. dev_priv->gt.init_rings = i915_gem_init_rings;
  4000. dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
  4001. dev_priv->gt.stop_ring = intel_stop_ring_buffer;
  4002. } else {
  4003. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4004. dev_priv->gt.init_rings = intel_logical_rings_init;
  4005. dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
  4006. dev_priv->gt.stop_ring = intel_logical_ring_stop;
  4007. }
  4008. /* This is just a security blanket to placate dragons.
  4009. * On some systems, we very sporadically observe that the first TLBs
  4010. * used by the CS may be stale, despite us poking the TLB reset. If
  4011. * we hold the forcewake during initialisation these problems
  4012. * just magically go away.
  4013. */
  4014. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4015. ret = i915_gem_init_userptr(dev);
  4016. if (ret)
  4017. goto out_unlock;
  4018. i915_gem_init_global_gtt(dev);
  4019. ret = i915_gem_context_init(dev);
  4020. if (ret)
  4021. goto out_unlock;
  4022. ret = dev_priv->gt.init_rings(dev);
  4023. if (ret)
  4024. goto out_unlock;
  4025. ret = i915_gem_init_hw(dev);
  4026. if (ret == -EIO) {
  4027. /* Allow ring initialisation to fail by marking the GPU as
  4028. * wedged. But we only want to do this where the GPU is angry,
  4029. * for all other failure, such as an allocation failure, bail.
  4030. */
  4031. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4032. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4033. ret = 0;
  4034. }
  4035. out_unlock:
  4036. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4037. mutex_unlock(&dev->struct_mutex);
  4038. return ret;
  4039. }
  4040. void
  4041. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4042. {
  4043. struct drm_i915_private *dev_priv = dev->dev_private;
  4044. struct intel_engine_cs *ring;
  4045. int i;
  4046. for_each_ring(ring, dev_priv, i)
  4047. dev_priv->gt.cleanup_ring(ring);
  4048. if (i915.enable_execlists)
  4049. /*
  4050. * Neither the BIOS, ourselves or any other kernel
  4051. * expects the system to be in execlists mode on startup,
  4052. * so we need to reset the GPU back to legacy mode.
  4053. */
  4054. intel_gpu_reset(dev);
  4055. }
  4056. static void
  4057. init_ring_lists(struct intel_engine_cs *ring)
  4058. {
  4059. INIT_LIST_HEAD(&ring->active_list);
  4060. INIT_LIST_HEAD(&ring->request_list);
  4061. }
  4062. void
  4063. i915_gem_load(struct drm_device *dev)
  4064. {
  4065. struct drm_i915_private *dev_priv = dev->dev_private;
  4066. int i;
  4067. dev_priv->objects =
  4068. kmem_cache_create("i915_gem_object",
  4069. sizeof(struct drm_i915_gem_object), 0,
  4070. SLAB_HWCACHE_ALIGN,
  4071. NULL);
  4072. dev_priv->vmas =
  4073. kmem_cache_create("i915_gem_vma",
  4074. sizeof(struct i915_vma), 0,
  4075. SLAB_HWCACHE_ALIGN,
  4076. NULL);
  4077. dev_priv->requests =
  4078. kmem_cache_create("i915_gem_request",
  4079. sizeof(struct drm_i915_gem_request), 0,
  4080. SLAB_HWCACHE_ALIGN,
  4081. NULL);
  4082. INIT_LIST_HEAD(&dev_priv->vm_list);
  4083. INIT_LIST_HEAD(&dev_priv->context_list);
  4084. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4085. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4086. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4087. for (i = 0; i < I915_NUM_RINGS; i++)
  4088. init_ring_lists(&dev_priv->ring[i]);
  4089. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4090. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4091. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4092. i915_gem_retire_work_handler);
  4093. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4094. i915_gem_idle_work_handler);
  4095. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4096. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4097. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4098. dev_priv->num_fence_regs = 32;
  4099. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4100. dev_priv->num_fence_regs = 16;
  4101. else
  4102. dev_priv->num_fence_regs = 8;
  4103. if (intel_vgpu_active(dev))
  4104. dev_priv->num_fence_regs =
  4105. I915_READ(vgtif_reg(avail_rs.fence_num));
  4106. /*
  4107. * Set initial sequence number for requests.
  4108. * Using this number allows the wraparound to happen early,
  4109. * catching any obvious problems.
  4110. */
  4111. dev_priv->next_seqno = ((u32)~0 - 0x1100);
  4112. dev_priv->last_seqno = ((u32)~0 - 0x1101);
  4113. /* Initialize fence registers to zero */
  4114. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4115. i915_gem_restore_fences(dev);
  4116. i915_gem_detect_bit_6_swizzle(dev);
  4117. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4118. dev_priv->mm.interruptible = true;
  4119. i915_gem_shrinker_init(dev_priv);
  4120. mutex_init(&dev_priv->fb_tracking.lock);
  4121. }
  4122. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4123. {
  4124. struct drm_i915_file_private *file_priv = file->driver_priv;
  4125. /* Clean up our request list when the client is going away, so that
  4126. * later retire_requests won't dereference our soon-to-be-gone
  4127. * file_priv.
  4128. */
  4129. spin_lock(&file_priv->mm.lock);
  4130. while (!list_empty(&file_priv->mm.request_list)) {
  4131. struct drm_i915_gem_request *request;
  4132. request = list_first_entry(&file_priv->mm.request_list,
  4133. struct drm_i915_gem_request,
  4134. client_list);
  4135. list_del(&request->client_list);
  4136. request->file_priv = NULL;
  4137. }
  4138. spin_unlock(&file_priv->mm.lock);
  4139. if (!list_empty(&file_priv->rps.link)) {
  4140. spin_lock(&to_i915(dev)->rps.client_lock);
  4141. list_del(&file_priv->rps.link);
  4142. spin_unlock(&to_i915(dev)->rps.client_lock);
  4143. }
  4144. }
  4145. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4146. {
  4147. struct drm_i915_file_private *file_priv;
  4148. int ret;
  4149. DRM_DEBUG_DRIVER("\n");
  4150. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4151. if (!file_priv)
  4152. return -ENOMEM;
  4153. file->driver_priv = file_priv;
  4154. file_priv->dev_priv = dev->dev_private;
  4155. file_priv->file = file;
  4156. INIT_LIST_HEAD(&file_priv->rps.link);
  4157. spin_lock_init(&file_priv->mm.lock);
  4158. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4159. ret = i915_gem_context_open(dev, file);
  4160. if (ret)
  4161. kfree(file_priv);
  4162. return ret;
  4163. }
  4164. /**
  4165. * i915_gem_track_fb - update frontbuffer tracking
  4166. * @old: current GEM buffer for the frontbuffer slots
  4167. * @new: new GEM buffer for the frontbuffer slots
  4168. * @frontbuffer_bits: bitmask of frontbuffer slots
  4169. *
  4170. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4171. * from @old and setting them in @new. Both @old and @new can be NULL.
  4172. */
  4173. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4174. struct drm_i915_gem_object *new,
  4175. unsigned frontbuffer_bits)
  4176. {
  4177. if (old) {
  4178. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4179. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4180. old->frontbuffer_bits &= ~frontbuffer_bits;
  4181. }
  4182. if (new) {
  4183. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4184. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4185. new->frontbuffer_bits |= frontbuffer_bits;
  4186. }
  4187. }
  4188. /* All the new VM stuff */
  4189. u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4190. struct i915_address_space *vm)
  4191. {
  4192. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4193. struct i915_vma *vma;
  4194. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4195. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4196. if (i915_is_ggtt(vma->vm) &&
  4197. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4198. continue;
  4199. if (vma->vm == vm)
  4200. return vma->node.start;
  4201. }
  4202. WARN(1, "%s vma for this object not found.\n",
  4203. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4204. return -1;
  4205. }
  4206. u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4207. const struct i915_ggtt_view *view)
  4208. {
  4209. struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
  4210. struct i915_vma *vma;
  4211. list_for_each_entry(vma, &o->vma_list, vma_link)
  4212. if (vma->vm == ggtt &&
  4213. i915_ggtt_view_equal(&vma->ggtt_view, view))
  4214. return vma->node.start;
  4215. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4216. return -1;
  4217. }
  4218. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4219. struct i915_address_space *vm)
  4220. {
  4221. struct i915_vma *vma;
  4222. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4223. if (i915_is_ggtt(vma->vm) &&
  4224. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4225. continue;
  4226. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4227. return true;
  4228. }
  4229. return false;
  4230. }
  4231. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4232. const struct i915_ggtt_view *view)
  4233. {
  4234. struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
  4235. struct i915_vma *vma;
  4236. list_for_each_entry(vma, &o->vma_list, vma_link)
  4237. if (vma->vm == ggtt &&
  4238. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4239. drm_mm_node_allocated(&vma->node))
  4240. return true;
  4241. return false;
  4242. }
  4243. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4244. {
  4245. struct i915_vma *vma;
  4246. list_for_each_entry(vma, &o->vma_list, vma_link)
  4247. if (drm_mm_node_allocated(&vma->node))
  4248. return true;
  4249. return false;
  4250. }
  4251. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4252. struct i915_address_space *vm)
  4253. {
  4254. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4255. struct i915_vma *vma;
  4256. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4257. BUG_ON(list_empty(&o->vma_list));
  4258. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4259. if (i915_is_ggtt(vma->vm) &&
  4260. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4261. continue;
  4262. if (vma->vm == vm)
  4263. return vma->node.size;
  4264. }
  4265. return 0;
  4266. }
  4267. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4268. {
  4269. struct i915_vma *vma;
  4270. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4271. if (vma->pin_count > 0)
  4272. return true;
  4273. return false;
  4274. }
  4275. /* Allocate a new GEM object and fill it with the supplied data */
  4276. struct drm_i915_gem_object *
  4277. i915_gem_object_create_from_data(struct drm_device *dev,
  4278. const void *data, size_t size)
  4279. {
  4280. struct drm_i915_gem_object *obj;
  4281. struct sg_table *sg;
  4282. size_t bytes;
  4283. int ret;
  4284. obj = i915_gem_alloc_object(dev, round_up(size, PAGE_SIZE));
  4285. if (IS_ERR_OR_NULL(obj))
  4286. return obj;
  4287. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  4288. if (ret)
  4289. goto fail;
  4290. ret = i915_gem_object_get_pages(obj);
  4291. if (ret)
  4292. goto fail;
  4293. i915_gem_object_pin_pages(obj);
  4294. sg = obj->pages;
  4295. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4296. i915_gem_object_unpin_pages(obj);
  4297. if (WARN_ON(bytes != size)) {
  4298. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4299. ret = -EFAULT;
  4300. goto fail;
  4301. }
  4302. return obj;
  4303. fail:
  4304. drm_gem_object_unreference(&obj->base);
  4305. return ERR_PTR(ret);
  4306. }