i915_gem.c 140 KB

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