i915_gem.c 136 KB

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