i915_gem.c 138 KB

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