i915_gem.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. struct change_domains {
  37. uint32_t invalidate_domains;
  38. uint32_t flush_domains;
  39. uint32_t flush_rings;
  40. };
  41. static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
  42. static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
  43. static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
  44. bool pipelined);
  45. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  46. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  47. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  48. int write);
  49. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  50. uint64_t offset,
  51. uint64_t size);
  52. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  53. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
  54. bool interruptible);
  55. static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  56. unsigned alignment,
  57. bool map_and_fenceable);
  58. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  59. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  60. struct drm_i915_gem_pwrite *args,
  61. struct drm_file *file_priv);
  62. static void i915_gem_free_object_tail(struct drm_gem_object *obj);
  63. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  64. int nr_to_scan,
  65. gfp_t gfp_mask);
  66. /* some bookkeeping */
  67. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  68. size_t size)
  69. {
  70. dev_priv->mm.object_count++;
  71. dev_priv->mm.object_memory += size;
  72. }
  73. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  74. size_t size)
  75. {
  76. dev_priv->mm.object_count--;
  77. dev_priv->mm.object_memory -= size;
  78. }
  79. static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
  80. struct drm_i915_gem_object *obj)
  81. {
  82. dev_priv->mm.gtt_count++;
  83. dev_priv->mm.gtt_memory += obj->gtt_space->size;
  84. if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
  85. dev_priv->mm.mappable_gtt_used +=
  86. min_t(size_t, obj->gtt_space->size,
  87. dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
  88. }
  89. }
  90. static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
  91. struct drm_i915_gem_object *obj)
  92. {
  93. dev_priv->mm.gtt_count--;
  94. dev_priv->mm.gtt_memory -= obj->gtt_space->size;
  95. if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
  96. dev_priv->mm.mappable_gtt_used -=
  97. min_t(size_t, obj->gtt_space->size,
  98. dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
  99. }
  100. }
  101. /**
  102. * Update the mappable working set counters. Call _only_ when there is a change
  103. * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
  104. * @mappable: new state the changed mappable flag (either pin_ or fault_).
  105. */
  106. static void
  107. i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
  108. struct drm_i915_gem_object *obj,
  109. bool mappable)
  110. {
  111. if (mappable) {
  112. if (obj->pin_mappable && obj->fault_mappable)
  113. /* Combined state was already mappable. */
  114. return;
  115. dev_priv->mm.gtt_mappable_count++;
  116. dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
  117. } else {
  118. if (obj->pin_mappable || obj->fault_mappable)
  119. /* Combined state still mappable. */
  120. return;
  121. dev_priv->mm.gtt_mappable_count--;
  122. dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
  123. }
  124. }
  125. static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
  126. struct drm_i915_gem_object *obj,
  127. bool mappable)
  128. {
  129. dev_priv->mm.pin_count++;
  130. dev_priv->mm.pin_memory += obj->gtt_space->size;
  131. if (mappable) {
  132. obj->pin_mappable = true;
  133. i915_gem_info_update_mappable(dev_priv, obj, true);
  134. }
  135. }
  136. static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
  137. struct drm_i915_gem_object *obj)
  138. {
  139. dev_priv->mm.pin_count--;
  140. dev_priv->mm.pin_memory -= obj->gtt_space->size;
  141. if (obj->pin_mappable) {
  142. obj->pin_mappable = false;
  143. i915_gem_info_update_mappable(dev_priv, obj, false);
  144. }
  145. }
  146. int
  147. i915_gem_check_is_wedged(struct drm_device *dev)
  148. {
  149. struct drm_i915_private *dev_priv = dev->dev_private;
  150. struct completion *x = &dev_priv->error_completion;
  151. unsigned long flags;
  152. int ret;
  153. if (!atomic_read(&dev_priv->mm.wedged))
  154. return 0;
  155. ret = wait_for_completion_interruptible(x);
  156. if (ret)
  157. return ret;
  158. /* Success, we reset the GPU! */
  159. if (!atomic_read(&dev_priv->mm.wedged))
  160. return 0;
  161. /* GPU is hung, bump the completion count to account for
  162. * the token we just consumed so that we never hit zero and
  163. * end up waiting upon a subsequent completion event that
  164. * will never happen.
  165. */
  166. spin_lock_irqsave(&x->wait.lock, flags);
  167. x->done++;
  168. spin_unlock_irqrestore(&x->wait.lock, flags);
  169. return -EIO;
  170. }
  171. static int i915_mutex_lock_interruptible(struct drm_device *dev)
  172. {
  173. struct drm_i915_private *dev_priv = dev->dev_private;
  174. int ret;
  175. ret = i915_gem_check_is_wedged(dev);
  176. if (ret)
  177. return ret;
  178. ret = mutex_lock_interruptible(&dev->struct_mutex);
  179. if (ret)
  180. return ret;
  181. if (atomic_read(&dev_priv->mm.wedged)) {
  182. mutex_unlock(&dev->struct_mutex);
  183. return -EAGAIN;
  184. }
  185. WARN_ON(i915_verify_lists(dev));
  186. return 0;
  187. }
  188. static inline bool
  189. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
  190. {
  191. return obj_priv->gtt_space &&
  192. !obj_priv->active &&
  193. obj_priv->pin_count == 0;
  194. }
  195. int i915_gem_do_init(struct drm_device *dev,
  196. unsigned long start,
  197. unsigned long mappable_end,
  198. unsigned long end)
  199. {
  200. drm_i915_private_t *dev_priv = dev->dev_private;
  201. if (start >= end ||
  202. (start & (PAGE_SIZE - 1)) != 0 ||
  203. (end & (PAGE_SIZE - 1)) != 0) {
  204. return -EINVAL;
  205. }
  206. drm_mm_init(&dev_priv->mm.gtt_space, start,
  207. end - start);
  208. dev_priv->mm.gtt_total = end - start;
  209. dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
  210. dev_priv->mm.gtt_mappable_end = mappable_end;
  211. return 0;
  212. }
  213. int
  214. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  215. struct drm_file *file_priv)
  216. {
  217. struct drm_i915_gem_init *args = data;
  218. int ret;
  219. mutex_lock(&dev->struct_mutex);
  220. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
  221. mutex_unlock(&dev->struct_mutex);
  222. return ret;
  223. }
  224. int
  225. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  226. struct drm_file *file_priv)
  227. {
  228. struct drm_i915_private *dev_priv = dev->dev_private;
  229. struct drm_i915_gem_get_aperture *args = data;
  230. if (!(dev->driver->driver_features & DRIVER_GEM))
  231. return -ENODEV;
  232. mutex_lock(&dev->struct_mutex);
  233. args->aper_size = dev_priv->mm.gtt_total;
  234. args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
  235. mutex_unlock(&dev->struct_mutex);
  236. return 0;
  237. }
  238. /**
  239. * Creates a new mm object and returns a handle to it.
  240. */
  241. int
  242. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  243. struct drm_file *file_priv)
  244. {
  245. struct drm_i915_gem_create *args = data;
  246. struct drm_gem_object *obj;
  247. int ret;
  248. u32 handle;
  249. args->size = roundup(args->size, PAGE_SIZE);
  250. /* Allocate the new object */
  251. obj = i915_gem_alloc_object(dev, args->size);
  252. if (obj == NULL)
  253. return -ENOMEM;
  254. ret = drm_gem_handle_create(file_priv, obj, &handle);
  255. if (ret) {
  256. drm_gem_object_release(obj);
  257. i915_gem_info_remove_obj(dev->dev_private, obj->size);
  258. kfree(obj);
  259. return ret;
  260. }
  261. /* drop reference from allocate - handle holds it now */
  262. drm_gem_object_unreference(obj);
  263. trace_i915_gem_object_create(obj);
  264. args->handle = handle;
  265. return 0;
  266. }
  267. static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
  268. {
  269. drm_i915_private_t *dev_priv = obj->dev->dev_private;
  270. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  271. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  272. obj_priv->tiling_mode != I915_TILING_NONE;
  273. }
  274. static inline void
  275. slow_shmem_copy(struct page *dst_page,
  276. int dst_offset,
  277. struct page *src_page,
  278. int src_offset,
  279. int length)
  280. {
  281. char *dst_vaddr, *src_vaddr;
  282. dst_vaddr = kmap(dst_page);
  283. src_vaddr = kmap(src_page);
  284. memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
  285. kunmap(src_page);
  286. kunmap(dst_page);
  287. }
  288. static inline void
  289. slow_shmem_bit17_copy(struct page *gpu_page,
  290. int gpu_offset,
  291. struct page *cpu_page,
  292. int cpu_offset,
  293. int length,
  294. int is_read)
  295. {
  296. char *gpu_vaddr, *cpu_vaddr;
  297. /* Use the unswizzled path if this page isn't affected. */
  298. if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
  299. if (is_read)
  300. return slow_shmem_copy(cpu_page, cpu_offset,
  301. gpu_page, gpu_offset, length);
  302. else
  303. return slow_shmem_copy(gpu_page, gpu_offset,
  304. cpu_page, cpu_offset, length);
  305. }
  306. gpu_vaddr = kmap(gpu_page);
  307. cpu_vaddr = kmap(cpu_page);
  308. /* Copy the data, XORing A6 with A17 (1). The user already knows he's
  309. * XORing with the other bits (A9 for Y, A9 and A10 for X)
  310. */
  311. while (length > 0) {
  312. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  313. int this_length = min(cacheline_end - gpu_offset, length);
  314. int swizzled_gpu_offset = gpu_offset ^ 64;
  315. if (is_read) {
  316. memcpy(cpu_vaddr + cpu_offset,
  317. gpu_vaddr + swizzled_gpu_offset,
  318. this_length);
  319. } else {
  320. memcpy(gpu_vaddr + swizzled_gpu_offset,
  321. cpu_vaddr + cpu_offset,
  322. this_length);
  323. }
  324. cpu_offset += this_length;
  325. gpu_offset += this_length;
  326. length -= this_length;
  327. }
  328. kunmap(cpu_page);
  329. kunmap(gpu_page);
  330. }
  331. /**
  332. * This is the fast shmem pread path, which attempts to copy_from_user directly
  333. * from the backing pages of the object to the user's address space. On a
  334. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  335. */
  336. static int
  337. i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
  338. struct drm_i915_gem_pread *args,
  339. struct drm_file *file_priv)
  340. {
  341. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  342. struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
  343. ssize_t remain;
  344. loff_t offset;
  345. char __user *user_data;
  346. int page_offset, page_length;
  347. user_data = (char __user *) (uintptr_t) args->data_ptr;
  348. remain = args->size;
  349. obj_priv = to_intel_bo(obj);
  350. offset = args->offset;
  351. while (remain > 0) {
  352. struct page *page;
  353. char *vaddr;
  354. int ret;
  355. /* Operation in this page
  356. *
  357. * page_offset = offset within page
  358. * page_length = bytes to copy for this page
  359. */
  360. page_offset = offset & (PAGE_SIZE-1);
  361. page_length = remain;
  362. if ((page_offset + remain) > PAGE_SIZE)
  363. page_length = PAGE_SIZE - page_offset;
  364. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  365. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  366. if (IS_ERR(page))
  367. return PTR_ERR(page);
  368. vaddr = kmap_atomic(page);
  369. ret = __copy_to_user_inatomic(user_data,
  370. vaddr + page_offset,
  371. page_length);
  372. kunmap_atomic(vaddr);
  373. mark_page_accessed(page);
  374. page_cache_release(page);
  375. if (ret)
  376. return -EFAULT;
  377. remain -= page_length;
  378. user_data += page_length;
  379. offset += page_length;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * This is the fallback shmem pread path, which allocates temporary storage
  385. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  386. * can copy out of the object's backing pages while holding the struct mutex
  387. * and not take page faults.
  388. */
  389. static int
  390. i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
  391. struct drm_i915_gem_pread *args,
  392. struct drm_file *file_priv)
  393. {
  394. struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
  395. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  396. struct mm_struct *mm = current->mm;
  397. struct page **user_pages;
  398. ssize_t remain;
  399. loff_t offset, pinned_pages, i;
  400. loff_t first_data_page, last_data_page, num_pages;
  401. int shmem_page_offset;
  402. int data_page_index, data_page_offset;
  403. int page_length;
  404. int ret;
  405. uint64_t data_ptr = args->data_ptr;
  406. int do_bit17_swizzling;
  407. remain = args->size;
  408. /* Pin the user pages containing the data. We can't fault while
  409. * holding the struct mutex, yet we want to hold it while
  410. * dereferencing the user data.
  411. */
  412. first_data_page = data_ptr / PAGE_SIZE;
  413. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  414. num_pages = last_data_page - first_data_page + 1;
  415. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  416. if (user_pages == NULL)
  417. return -ENOMEM;
  418. mutex_unlock(&dev->struct_mutex);
  419. down_read(&mm->mmap_sem);
  420. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  421. num_pages, 1, 0, user_pages, NULL);
  422. up_read(&mm->mmap_sem);
  423. mutex_lock(&dev->struct_mutex);
  424. if (pinned_pages < num_pages) {
  425. ret = -EFAULT;
  426. goto out;
  427. }
  428. ret = i915_gem_object_set_cpu_read_domain_range(obj,
  429. args->offset,
  430. args->size);
  431. if (ret)
  432. goto out;
  433. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  434. obj_priv = to_intel_bo(obj);
  435. offset = args->offset;
  436. while (remain > 0) {
  437. struct page *page;
  438. /* Operation in this page
  439. *
  440. * shmem_page_offset = offset within page in shmem file
  441. * data_page_index = page number in get_user_pages return
  442. * data_page_offset = offset with data_page_index page.
  443. * page_length = bytes to copy for this page
  444. */
  445. shmem_page_offset = offset & ~PAGE_MASK;
  446. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  447. data_page_offset = data_ptr & ~PAGE_MASK;
  448. page_length = remain;
  449. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  450. page_length = PAGE_SIZE - shmem_page_offset;
  451. if ((data_page_offset + page_length) > PAGE_SIZE)
  452. page_length = PAGE_SIZE - data_page_offset;
  453. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  454. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  455. if (IS_ERR(page))
  456. return PTR_ERR(page);
  457. if (do_bit17_swizzling) {
  458. slow_shmem_bit17_copy(page,
  459. shmem_page_offset,
  460. user_pages[data_page_index],
  461. data_page_offset,
  462. page_length,
  463. 1);
  464. } else {
  465. slow_shmem_copy(user_pages[data_page_index],
  466. data_page_offset,
  467. page,
  468. shmem_page_offset,
  469. page_length);
  470. }
  471. mark_page_accessed(page);
  472. page_cache_release(page);
  473. remain -= page_length;
  474. data_ptr += page_length;
  475. offset += page_length;
  476. }
  477. out:
  478. for (i = 0; i < pinned_pages; i++) {
  479. SetPageDirty(user_pages[i]);
  480. mark_page_accessed(user_pages[i]);
  481. page_cache_release(user_pages[i]);
  482. }
  483. drm_free_large(user_pages);
  484. return ret;
  485. }
  486. /**
  487. * Reads data from the object referenced by handle.
  488. *
  489. * On error, the contents of *data are undefined.
  490. */
  491. int
  492. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  493. struct drm_file *file_priv)
  494. {
  495. struct drm_i915_gem_pread *args = data;
  496. struct drm_gem_object *obj;
  497. struct drm_i915_gem_object *obj_priv;
  498. int ret = 0;
  499. if (args->size == 0)
  500. return 0;
  501. if (!access_ok(VERIFY_WRITE,
  502. (char __user *)(uintptr_t)args->data_ptr,
  503. args->size))
  504. return -EFAULT;
  505. ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
  506. args->size);
  507. if (ret)
  508. return -EFAULT;
  509. ret = i915_mutex_lock_interruptible(dev);
  510. if (ret)
  511. return ret;
  512. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  513. if (obj == NULL) {
  514. ret = -ENOENT;
  515. goto unlock;
  516. }
  517. obj_priv = to_intel_bo(obj);
  518. /* Bounds check source. */
  519. if (args->offset > obj->size || args->size > obj->size - args->offset) {
  520. ret = -EINVAL;
  521. goto out;
  522. }
  523. ret = i915_gem_object_set_cpu_read_domain_range(obj,
  524. args->offset,
  525. args->size);
  526. if (ret)
  527. goto out;
  528. ret = -EFAULT;
  529. if (!i915_gem_object_needs_bit17_swizzle(obj))
  530. ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
  531. if (ret == -EFAULT)
  532. ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
  533. out:
  534. drm_gem_object_unreference(obj);
  535. unlock:
  536. mutex_unlock(&dev->struct_mutex);
  537. return ret;
  538. }
  539. /* This is the fast write path which cannot handle
  540. * page faults in the source data
  541. */
  542. static inline int
  543. fast_user_write(struct io_mapping *mapping,
  544. loff_t page_base, int page_offset,
  545. char __user *user_data,
  546. int length)
  547. {
  548. char *vaddr_atomic;
  549. unsigned long unwritten;
  550. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  551. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  552. user_data, length);
  553. io_mapping_unmap_atomic(vaddr_atomic);
  554. return unwritten;
  555. }
  556. /* Here's the write path which can sleep for
  557. * page faults
  558. */
  559. static inline void
  560. slow_kernel_write(struct io_mapping *mapping,
  561. loff_t gtt_base, int gtt_offset,
  562. struct page *user_page, int user_offset,
  563. int length)
  564. {
  565. char __iomem *dst_vaddr;
  566. char *src_vaddr;
  567. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  568. src_vaddr = kmap(user_page);
  569. memcpy_toio(dst_vaddr + gtt_offset,
  570. src_vaddr + user_offset,
  571. length);
  572. kunmap(user_page);
  573. io_mapping_unmap(dst_vaddr);
  574. }
  575. /**
  576. * This is the fast pwrite path, where we copy the data directly from the
  577. * user into the GTT, uncached.
  578. */
  579. static int
  580. i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  581. struct drm_i915_gem_pwrite *args,
  582. struct drm_file *file_priv)
  583. {
  584. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  585. drm_i915_private_t *dev_priv = dev->dev_private;
  586. ssize_t remain;
  587. loff_t offset, page_base;
  588. char __user *user_data;
  589. int page_offset, page_length;
  590. user_data = (char __user *) (uintptr_t) args->data_ptr;
  591. remain = args->size;
  592. obj_priv = to_intel_bo(obj);
  593. offset = obj_priv->gtt_offset + args->offset;
  594. while (remain > 0) {
  595. /* Operation in this page
  596. *
  597. * page_base = page offset within aperture
  598. * page_offset = offset within page
  599. * page_length = bytes to copy for this page
  600. */
  601. page_base = (offset & ~(PAGE_SIZE-1));
  602. page_offset = offset & (PAGE_SIZE-1);
  603. page_length = remain;
  604. if ((page_offset + remain) > PAGE_SIZE)
  605. page_length = PAGE_SIZE - page_offset;
  606. /* If we get a fault while copying data, then (presumably) our
  607. * source page isn't available. Return the error and we'll
  608. * retry in the slow path.
  609. */
  610. if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
  611. page_offset, user_data, page_length))
  612. return -EFAULT;
  613. remain -= page_length;
  614. user_data += page_length;
  615. offset += page_length;
  616. }
  617. return 0;
  618. }
  619. /**
  620. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  621. * the memory and maps it using kmap_atomic for copying.
  622. *
  623. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  624. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  625. */
  626. static int
  627. i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  628. struct drm_i915_gem_pwrite *args,
  629. struct drm_file *file_priv)
  630. {
  631. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  632. drm_i915_private_t *dev_priv = dev->dev_private;
  633. ssize_t remain;
  634. loff_t gtt_page_base, offset;
  635. loff_t first_data_page, last_data_page, num_pages;
  636. loff_t pinned_pages, i;
  637. struct page **user_pages;
  638. struct mm_struct *mm = current->mm;
  639. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  640. int ret;
  641. uint64_t data_ptr = args->data_ptr;
  642. remain = args->size;
  643. /* Pin the user pages containing the data. We can't fault while
  644. * holding the struct mutex, and all of the pwrite implementations
  645. * want to hold it while dereferencing the user data.
  646. */
  647. first_data_page = data_ptr / PAGE_SIZE;
  648. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  649. num_pages = last_data_page - first_data_page + 1;
  650. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  651. if (user_pages == NULL)
  652. return -ENOMEM;
  653. mutex_unlock(&dev->struct_mutex);
  654. down_read(&mm->mmap_sem);
  655. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  656. num_pages, 0, 0, user_pages, NULL);
  657. up_read(&mm->mmap_sem);
  658. mutex_lock(&dev->struct_mutex);
  659. if (pinned_pages < num_pages) {
  660. ret = -EFAULT;
  661. goto out_unpin_pages;
  662. }
  663. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  664. if (ret)
  665. goto out_unpin_pages;
  666. obj_priv = to_intel_bo(obj);
  667. offset = obj_priv->gtt_offset + args->offset;
  668. while (remain > 0) {
  669. /* Operation in this page
  670. *
  671. * gtt_page_base = page offset within aperture
  672. * gtt_page_offset = offset within page in aperture
  673. * data_page_index = page number in get_user_pages return
  674. * data_page_offset = offset with data_page_index page.
  675. * page_length = bytes to copy for this page
  676. */
  677. gtt_page_base = offset & PAGE_MASK;
  678. gtt_page_offset = offset & ~PAGE_MASK;
  679. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  680. data_page_offset = data_ptr & ~PAGE_MASK;
  681. page_length = remain;
  682. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  683. page_length = PAGE_SIZE - gtt_page_offset;
  684. if ((data_page_offset + page_length) > PAGE_SIZE)
  685. page_length = PAGE_SIZE - data_page_offset;
  686. slow_kernel_write(dev_priv->mm.gtt_mapping,
  687. gtt_page_base, gtt_page_offset,
  688. user_pages[data_page_index],
  689. data_page_offset,
  690. page_length);
  691. remain -= page_length;
  692. offset += page_length;
  693. data_ptr += page_length;
  694. }
  695. out_unpin_pages:
  696. for (i = 0; i < pinned_pages; i++)
  697. page_cache_release(user_pages[i]);
  698. drm_free_large(user_pages);
  699. return ret;
  700. }
  701. /**
  702. * This is the fast shmem pwrite path, which attempts to directly
  703. * copy_from_user into the kmapped pages backing the object.
  704. */
  705. static int
  706. i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  707. struct drm_i915_gem_pwrite *args,
  708. struct drm_file *file_priv)
  709. {
  710. struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
  711. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  712. ssize_t remain;
  713. loff_t offset;
  714. char __user *user_data;
  715. int page_offset, page_length;
  716. user_data = (char __user *) (uintptr_t) args->data_ptr;
  717. remain = args->size;
  718. obj_priv = to_intel_bo(obj);
  719. offset = args->offset;
  720. obj_priv->dirty = 1;
  721. while (remain > 0) {
  722. struct page *page;
  723. char *vaddr;
  724. int ret;
  725. /* Operation in this page
  726. *
  727. * page_offset = offset within page
  728. * page_length = bytes to copy for this page
  729. */
  730. page_offset = offset & (PAGE_SIZE-1);
  731. page_length = remain;
  732. if ((page_offset + remain) > PAGE_SIZE)
  733. page_length = PAGE_SIZE - page_offset;
  734. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  735. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  736. if (IS_ERR(page))
  737. return PTR_ERR(page);
  738. vaddr = kmap_atomic(page, KM_USER0);
  739. ret = __copy_from_user_inatomic(vaddr + page_offset,
  740. user_data,
  741. page_length);
  742. kunmap_atomic(vaddr, KM_USER0);
  743. set_page_dirty(page);
  744. mark_page_accessed(page);
  745. page_cache_release(page);
  746. /* If we get a fault while copying data, then (presumably) our
  747. * source page isn't available. Return the error and we'll
  748. * retry in the slow path.
  749. */
  750. if (ret)
  751. return -EFAULT;
  752. remain -= page_length;
  753. user_data += page_length;
  754. offset += page_length;
  755. }
  756. return 0;
  757. }
  758. /**
  759. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  760. * the memory and maps it using kmap_atomic for copying.
  761. *
  762. * This avoids taking mmap_sem for faulting on the user's address while the
  763. * struct_mutex is held.
  764. */
  765. static int
  766. i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  767. struct drm_i915_gem_pwrite *args,
  768. struct drm_file *file_priv)
  769. {
  770. struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
  771. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  772. struct mm_struct *mm = current->mm;
  773. struct page **user_pages;
  774. ssize_t remain;
  775. loff_t offset, pinned_pages, i;
  776. loff_t first_data_page, last_data_page, num_pages;
  777. int shmem_page_offset;
  778. int data_page_index, data_page_offset;
  779. int page_length;
  780. int ret;
  781. uint64_t data_ptr = args->data_ptr;
  782. int do_bit17_swizzling;
  783. remain = args->size;
  784. /* Pin the user pages containing the data. We can't fault while
  785. * holding the struct mutex, and all of the pwrite implementations
  786. * want to hold it while dereferencing the user data.
  787. */
  788. first_data_page = data_ptr / PAGE_SIZE;
  789. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  790. num_pages = last_data_page - first_data_page + 1;
  791. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  792. if (user_pages == NULL)
  793. return -ENOMEM;
  794. mutex_unlock(&dev->struct_mutex);
  795. down_read(&mm->mmap_sem);
  796. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  797. num_pages, 0, 0, user_pages, NULL);
  798. up_read(&mm->mmap_sem);
  799. mutex_lock(&dev->struct_mutex);
  800. if (pinned_pages < num_pages) {
  801. ret = -EFAULT;
  802. goto out;
  803. }
  804. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  805. if (ret)
  806. goto out;
  807. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  808. obj_priv = to_intel_bo(obj);
  809. offset = args->offset;
  810. obj_priv->dirty = 1;
  811. while (remain > 0) {
  812. struct page *page;
  813. /* Operation in this page
  814. *
  815. * shmem_page_offset = offset within page in shmem file
  816. * data_page_index = page number in get_user_pages return
  817. * data_page_offset = offset with data_page_index page.
  818. * page_length = bytes to copy for this page
  819. */
  820. shmem_page_offset = offset & ~PAGE_MASK;
  821. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  822. data_page_offset = data_ptr & ~PAGE_MASK;
  823. page_length = remain;
  824. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  825. page_length = PAGE_SIZE - shmem_page_offset;
  826. if ((data_page_offset + page_length) > PAGE_SIZE)
  827. page_length = PAGE_SIZE - data_page_offset;
  828. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  829. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  830. if (IS_ERR(page)) {
  831. ret = PTR_ERR(page);
  832. goto out;
  833. }
  834. if (do_bit17_swizzling) {
  835. slow_shmem_bit17_copy(page,
  836. shmem_page_offset,
  837. user_pages[data_page_index],
  838. data_page_offset,
  839. page_length,
  840. 0);
  841. } else {
  842. slow_shmem_copy(page,
  843. shmem_page_offset,
  844. user_pages[data_page_index],
  845. data_page_offset,
  846. page_length);
  847. }
  848. set_page_dirty(page);
  849. mark_page_accessed(page);
  850. page_cache_release(page);
  851. remain -= page_length;
  852. data_ptr += page_length;
  853. offset += page_length;
  854. }
  855. out:
  856. for (i = 0; i < pinned_pages; i++)
  857. page_cache_release(user_pages[i]);
  858. drm_free_large(user_pages);
  859. return ret;
  860. }
  861. /**
  862. * Writes data to the object referenced by handle.
  863. *
  864. * On error, the contents of the buffer that were to be modified are undefined.
  865. */
  866. int
  867. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  868. struct drm_file *file)
  869. {
  870. struct drm_i915_gem_pwrite *args = data;
  871. struct drm_gem_object *obj;
  872. struct drm_i915_gem_object *obj_priv;
  873. int ret;
  874. if (args->size == 0)
  875. return 0;
  876. if (!access_ok(VERIFY_READ,
  877. (char __user *)(uintptr_t)args->data_ptr,
  878. args->size))
  879. return -EFAULT;
  880. ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
  881. args->size);
  882. if (ret)
  883. return -EFAULT;
  884. ret = i915_mutex_lock_interruptible(dev);
  885. if (ret)
  886. return ret;
  887. obj = drm_gem_object_lookup(dev, file, args->handle);
  888. if (obj == NULL) {
  889. ret = -ENOENT;
  890. goto unlock;
  891. }
  892. obj_priv = to_intel_bo(obj);
  893. /* Bounds check destination. */
  894. if (args->offset > obj->size || args->size > obj->size - args->offset) {
  895. ret = -EINVAL;
  896. goto out;
  897. }
  898. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  899. * it would end up going through the fenced access, and we'll get
  900. * different detiling behavior between reading and writing.
  901. * pread/pwrite currently are reading and writing from the CPU
  902. * perspective, requiring manual detiling by the client.
  903. */
  904. if (obj_priv->phys_obj)
  905. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  906. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  907. obj_priv->gtt_space &&
  908. obj->write_domain != I915_GEM_DOMAIN_CPU) {
  909. ret = i915_gem_object_pin(obj, 0, true);
  910. if (ret)
  911. goto out;
  912. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  913. if (ret)
  914. goto out_unpin;
  915. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  916. if (ret == -EFAULT)
  917. ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
  918. out_unpin:
  919. i915_gem_object_unpin(obj);
  920. } else {
  921. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  922. if (ret)
  923. goto out;
  924. ret = -EFAULT;
  925. if (!i915_gem_object_needs_bit17_swizzle(obj))
  926. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
  927. if (ret == -EFAULT)
  928. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
  929. }
  930. out:
  931. drm_gem_object_unreference(obj);
  932. unlock:
  933. mutex_unlock(&dev->struct_mutex);
  934. return ret;
  935. }
  936. /**
  937. * Called when user space prepares to use an object with the CPU, either
  938. * through the mmap ioctl's mapping or a GTT mapping.
  939. */
  940. int
  941. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  942. struct drm_file *file_priv)
  943. {
  944. struct drm_i915_private *dev_priv = dev->dev_private;
  945. struct drm_i915_gem_set_domain *args = data;
  946. struct drm_gem_object *obj;
  947. struct drm_i915_gem_object *obj_priv;
  948. uint32_t read_domains = args->read_domains;
  949. uint32_t write_domain = args->write_domain;
  950. int ret;
  951. if (!(dev->driver->driver_features & DRIVER_GEM))
  952. return -ENODEV;
  953. /* Only handle setting domains to types used by the CPU. */
  954. if (write_domain & I915_GEM_GPU_DOMAINS)
  955. return -EINVAL;
  956. if (read_domains & I915_GEM_GPU_DOMAINS)
  957. return -EINVAL;
  958. /* Having something in the write domain implies it's in the read
  959. * domain, and only that read domain. Enforce that in the request.
  960. */
  961. if (write_domain != 0 && read_domains != write_domain)
  962. return -EINVAL;
  963. ret = i915_mutex_lock_interruptible(dev);
  964. if (ret)
  965. return ret;
  966. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  967. if (obj == NULL) {
  968. ret = -ENOENT;
  969. goto unlock;
  970. }
  971. obj_priv = to_intel_bo(obj);
  972. intel_mark_busy(dev, obj);
  973. if (read_domains & I915_GEM_DOMAIN_GTT) {
  974. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  975. /* Update the LRU on the fence for the CPU access that's
  976. * about to occur.
  977. */
  978. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  979. struct drm_i915_fence_reg *reg =
  980. &dev_priv->fence_regs[obj_priv->fence_reg];
  981. list_move_tail(&reg->lru_list,
  982. &dev_priv->mm.fence_list);
  983. }
  984. /* Silently promote "you're not bound, there was nothing to do"
  985. * to success, since the client was just asking us to
  986. * make sure everything was done.
  987. */
  988. if (ret == -EINVAL)
  989. ret = 0;
  990. } else {
  991. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  992. }
  993. /* Maintain LRU order of "inactive" objects */
  994. if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
  995. list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
  996. drm_gem_object_unreference(obj);
  997. unlock:
  998. mutex_unlock(&dev->struct_mutex);
  999. return ret;
  1000. }
  1001. /**
  1002. * Called when user space has done writes to this buffer
  1003. */
  1004. int
  1005. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1006. struct drm_file *file_priv)
  1007. {
  1008. struct drm_i915_gem_sw_finish *args = data;
  1009. struct drm_gem_object *obj;
  1010. int ret = 0;
  1011. if (!(dev->driver->driver_features & DRIVER_GEM))
  1012. return -ENODEV;
  1013. ret = i915_mutex_lock_interruptible(dev);
  1014. if (ret)
  1015. return ret;
  1016. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1017. if (obj == NULL) {
  1018. ret = -ENOENT;
  1019. goto unlock;
  1020. }
  1021. /* Pinned buffers may be scanout, so flush the cache */
  1022. if (to_intel_bo(obj)->pin_count)
  1023. i915_gem_object_flush_cpu_write_domain(obj);
  1024. drm_gem_object_unreference(obj);
  1025. unlock:
  1026. mutex_unlock(&dev->struct_mutex);
  1027. return ret;
  1028. }
  1029. /**
  1030. * Maps the contents of an object, returning the address it is mapped
  1031. * into.
  1032. *
  1033. * While the mapping holds a reference on the contents of the object, it doesn't
  1034. * imply a ref on the object itself.
  1035. */
  1036. int
  1037. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1038. struct drm_file *file_priv)
  1039. {
  1040. struct drm_i915_private *dev_priv = dev->dev_private;
  1041. struct drm_i915_gem_mmap *args = data;
  1042. struct drm_gem_object *obj;
  1043. loff_t offset;
  1044. unsigned long addr;
  1045. if (!(dev->driver->driver_features & DRIVER_GEM))
  1046. return -ENODEV;
  1047. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1048. if (obj == NULL)
  1049. return -ENOENT;
  1050. if (obj->size > dev_priv->mm.gtt_mappable_end) {
  1051. drm_gem_object_unreference_unlocked(obj);
  1052. return -E2BIG;
  1053. }
  1054. offset = args->offset;
  1055. down_write(&current->mm->mmap_sem);
  1056. addr = do_mmap(obj->filp, 0, args->size,
  1057. PROT_READ | PROT_WRITE, MAP_SHARED,
  1058. args->offset);
  1059. up_write(&current->mm->mmap_sem);
  1060. drm_gem_object_unreference_unlocked(obj);
  1061. if (IS_ERR((void *)addr))
  1062. return addr;
  1063. args->addr_ptr = (uint64_t) addr;
  1064. return 0;
  1065. }
  1066. /**
  1067. * i915_gem_fault - fault a page into the GTT
  1068. * vma: VMA in question
  1069. * vmf: fault info
  1070. *
  1071. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1072. * from userspace. The fault handler takes care of binding the object to
  1073. * the GTT (if needed), allocating and programming a fence register (again,
  1074. * only if needed based on whether the old reg is still valid or the object
  1075. * is tiled) and inserting a new PTE into the faulting process.
  1076. *
  1077. * Note that the faulting process may involve evicting existing objects
  1078. * from the GTT and/or fence registers to make room. So performance may
  1079. * suffer if the GTT working set is large or there are few fence registers
  1080. * left.
  1081. */
  1082. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1083. {
  1084. struct drm_gem_object *obj = vma->vm_private_data;
  1085. struct drm_device *dev = obj->dev;
  1086. drm_i915_private_t *dev_priv = dev->dev_private;
  1087. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1088. pgoff_t page_offset;
  1089. unsigned long pfn;
  1090. int ret = 0;
  1091. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1092. /* We don't use vmf->pgoff since that has the fake offset */
  1093. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1094. PAGE_SHIFT;
  1095. /* Now bind it into the GTT if needed */
  1096. mutex_lock(&dev->struct_mutex);
  1097. BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
  1098. if (obj_priv->gtt_space) {
  1099. if (!obj_priv->map_and_fenceable) {
  1100. ret = i915_gem_object_unbind(obj);
  1101. if (ret)
  1102. goto unlock;
  1103. }
  1104. }
  1105. if (!obj_priv->gtt_space) {
  1106. ret = i915_gem_object_bind_to_gtt(obj, 0, true);
  1107. if (ret)
  1108. goto unlock;
  1109. }
  1110. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1111. if (ret)
  1112. goto unlock;
  1113. if (!obj_priv->fault_mappable) {
  1114. obj_priv->fault_mappable = true;
  1115. i915_gem_info_update_mappable(dev_priv, obj_priv, true);
  1116. }
  1117. /* Need a new fence register? */
  1118. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1119. ret = i915_gem_object_get_fence_reg(obj, true);
  1120. if (ret)
  1121. goto unlock;
  1122. }
  1123. if (i915_gem_object_is_inactive(obj_priv))
  1124. list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
  1125. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  1126. page_offset;
  1127. /* Finally, remap it using the new GTT offset */
  1128. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1129. unlock:
  1130. mutex_unlock(&dev->struct_mutex);
  1131. switch (ret) {
  1132. case -EAGAIN:
  1133. set_need_resched();
  1134. case 0:
  1135. case -ERESTARTSYS:
  1136. return VM_FAULT_NOPAGE;
  1137. case -ENOMEM:
  1138. return VM_FAULT_OOM;
  1139. default:
  1140. return VM_FAULT_SIGBUS;
  1141. }
  1142. }
  1143. /**
  1144. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  1145. * @obj: obj in question
  1146. *
  1147. * GEM memory mapping works by handing back to userspace a fake mmap offset
  1148. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  1149. * up the object based on the offset and sets up the various memory mapping
  1150. * structures.
  1151. *
  1152. * This routine allocates and attaches a fake offset for @obj.
  1153. */
  1154. static int
  1155. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  1156. {
  1157. struct drm_device *dev = obj->dev;
  1158. struct drm_gem_mm *mm = dev->mm_private;
  1159. struct drm_map_list *list;
  1160. struct drm_local_map *map;
  1161. int ret = 0;
  1162. /* Set the object up for mmap'ing */
  1163. list = &obj->map_list;
  1164. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  1165. if (!list->map)
  1166. return -ENOMEM;
  1167. map = list->map;
  1168. map->type = _DRM_GEM;
  1169. map->size = obj->size;
  1170. map->handle = obj;
  1171. /* Get a DRM GEM mmap offset allocated... */
  1172. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  1173. obj->size / PAGE_SIZE, 0, 0);
  1174. if (!list->file_offset_node) {
  1175. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  1176. ret = -ENOSPC;
  1177. goto out_free_list;
  1178. }
  1179. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  1180. obj->size / PAGE_SIZE, 0);
  1181. if (!list->file_offset_node) {
  1182. ret = -ENOMEM;
  1183. goto out_free_list;
  1184. }
  1185. list->hash.key = list->file_offset_node->start;
  1186. ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
  1187. if (ret) {
  1188. DRM_ERROR("failed to add to map hash\n");
  1189. goto out_free_mm;
  1190. }
  1191. return 0;
  1192. out_free_mm:
  1193. drm_mm_put_block(list->file_offset_node);
  1194. out_free_list:
  1195. kfree(list->map);
  1196. list->map = NULL;
  1197. return ret;
  1198. }
  1199. /**
  1200. * i915_gem_release_mmap - remove physical page mappings
  1201. * @obj: obj in question
  1202. *
  1203. * Preserve the reservation of the mmapping with the DRM core code, but
  1204. * relinquish ownership of the pages back to the system.
  1205. *
  1206. * It is vital that we remove the page mapping if we have mapped a tiled
  1207. * object through the GTT and then lose the fence register due to
  1208. * resource pressure. Similarly if the object has been moved out of the
  1209. * aperture, than pages mapped into userspace must be revoked. Removing the
  1210. * mapping will then trigger a page fault on the next user access, allowing
  1211. * fixup by i915_gem_fault().
  1212. */
  1213. void
  1214. i915_gem_release_mmap(struct drm_gem_object *obj)
  1215. {
  1216. struct drm_device *dev = obj->dev;
  1217. struct drm_i915_private *dev_priv = dev->dev_private;
  1218. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1219. if (unlikely(obj->map_list.map && dev->dev_mapping))
  1220. unmap_mapping_range(dev->dev_mapping,
  1221. (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
  1222. obj->size, 1);
  1223. if (obj_priv->fault_mappable) {
  1224. obj_priv->fault_mappable = false;
  1225. i915_gem_info_update_mappable(dev_priv, obj_priv, false);
  1226. }
  1227. }
  1228. static void
  1229. i915_gem_free_mmap_offset(struct drm_gem_object *obj)
  1230. {
  1231. struct drm_device *dev = obj->dev;
  1232. struct drm_gem_mm *mm = dev->mm_private;
  1233. struct drm_map_list *list = &obj->map_list;
  1234. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  1235. drm_mm_put_block(list->file_offset_node);
  1236. kfree(list->map);
  1237. list->map = NULL;
  1238. }
  1239. /**
  1240. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1241. * @obj: object to check
  1242. *
  1243. * Return the required GTT alignment for an object, taking into account
  1244. * potential fence register mapping.
  1245. */
  1246. static uint32_t
  1247. i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
  1248. {
  1249. struct drm_device *dev = obj_priv->base.dev;
  1250. /*
  1251. * Minimum alignment is 4k (GTT page size), but might be greater
  1252. * if a fence register is needed for the object.
  1253. */
  1254. if (INTEL_INFO(dev)->gen >= 4 ||
  1255. obj_priv->tiling_mode == I915_TILING_NONE)
  1256. return 4096;
  1257. /*
  1258. * Previous chips need to be aligned to the size of the smallest
  1259. * fence register that can contain the object.
  1260. */
  1261. return i915_gem_get_gtt_size(obj_priv);
  1262. }
  1263. /**
  1264. * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
  1265. * unfenced object
  1266. * @obj: object to check
  1267. *
  1268. * Return the required GTT alignment for an object, only taking into account
  1269. * unfenced tiled surface requirements.
  1270. */
  1271. static uint32_t
  1272. i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj_priv)
  1273. {
  1274. struct drm_device *dev = obj_priv->base.dev;
  1275. int tile_height;
  1276. /*
  1277. * Minimum alignment is 4k (GTT page size) for sane hw.
  1278. */
  1279. if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
  1280. obj_priv->tiling_mode == I915_TILING_NONE)
  1281. return 4096;
  1282. /*
  1283. * Older chips need unfenced tiled buffers to be aligned to the left
  1284. * edge of an even tile row (where tile rows are counted as if the bo is
  1285. * placed in a fenced gtt region).
  1286. */
  1287. if (IS_GEN2(dev) ||
  1288. (obj_priv->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  1289. tile_height = 32;
  1290. else
  1291. tile_height = 8;
  1292. return tile_height * obj_priv->stride * 2;
  1293. }
  1294. static uint32_t
  1295. i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
  1296. {
  1297. struct drm_device *dev = obj_priv->base.dev;
  1298. uint32_t size;
  1299. /*
  1300. * Minimum alignment is 4k (GTT page size), but might be greater
  1301. * if a fence register is needed for the object.
  1302. */
  1303. if (INTEL_INFO(dev)->gen >= 4)
  1304. return obj_priv->base.size;
  1305. /*
  1306. * Previous chips need to be aligned to the size of the smallest
  1307. * fence register that can contain the object.
  1308. */
  1309. if (INTEL_INFO(dev)->gen == 3)
  1310. size = 1024*1024;
  1311. else
  1312. size = 512*1024;
  1313. while (size < obj_priv->base.size)
  1314. size <<= 1;
  1315. return size;
  1316. }
  1317. /**
  1318. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1319. * @dev: DRM device
  1320. * @data: GTT mapping ioctl data
  1321. * @file_priv: GEM object info
  1322. *
  1323. * Simply returns the fake offset to userspace so it can mmap it.
  1324. * The mmap call will end up in drm_gem_mmap(), which will set things
  1325. * up so we can get faults in the handler above.
  1326. *
  1327. * The fault handler will take care of binding the object into the GTT
  1328. * (since it may have been evicted to make room for something), allocating
  1329. * a fence register, and mapping the appropriate aperture address into
  1330. * userspace.
  1331. */
  1332. int
  1333. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1334. struct drm_file *file_priv)
  1335. {
  1336. struct drm_i915_private *dev_priv = dev->dev_private;
  1337. struct drm_i915_gem_mmap_gtt *args = data;
  1338. struct drm_gem_object *obj;
  1339. struct drm_i915_gem_object *obj_priv;
  1340. int ret;
  1341. if (!(dev->driver->driver_features & DRIVER_GEM))
  1342. return -ENODEV;
  1343. ret = i915_mutex_lock_interruptible(dev);
  1344. if (ret)
  1345. return ret;
  1346. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1347. if (obj == NULL) {
  1348. ret = -ENOENT;
  1349. goto unlock;
  1350. }
  1351. obj_priv = to_intel_bo(obj);
  1352. if (obj->size > dev_priv->mm.gtt_mappable_end) {
  1353. ret = -E2BIG;
  1354. goto unlock;
  1355. }
  1356. if (obj_priv->madv != I915_MADV_WILLNEED) {
  1357. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1358. ret = -EINVAL;
  1359. goto out;
  1360. }
  1361. if (!obj->map_list.map) {
  1362. ret = i915_gem_create_mmap_offset(obj);
  1363. if (ret)
  1364. goto out;
  1365. }
  1366. args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
  1367. out:
  1368. drm_gem_object_unreference(obj);
  1369. unlock:
  1370. mutex_unlock(&dev->struct_mutex);
  1371. return ret;
  1372. }
  1373. static int
  1374. i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
  1375. gfp_t gfpmask)
  1376. {
  1377. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1378. int page_count, i;
  1379. struct address_space *mapping;
  1380. struct inode *inode;
  1381. struct page *page;
  1382. /* Get the list of pages out of our struct file. They'll be pinned
  1383. * at this point until we release them.
  1384. */
  1385. page_count = obj->size / PAGE_SIZE;
  1386. BUG_ON(obj_priv->pages != NULL);
  1387. obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
  1388. if (obj_priv->pages == NULL)
  1389. return -ENOMEM;
  1390. inode = obj->filp->f_path.dentry->d_inode;
  1391. mapping = inode->i_mapping;
  1392. for (i = 0; i < page_count; i++) {
  1393. page = read_cache_page_gfp(mapping, i,
  1394. GFP_HIGHUSER |
  1395. __GFP_COLD |
  1396. __GFP_RECLAIMABLE |
  1397. gfpmask);
  1398. if (IS_ERR(page))
  1399. goto err_pages;
  1400. obj_priv->pages[i] = page;
  1401. }
  1402. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1403. i915_gem_object_do_bit_17_swizzle(obj);
  1404. return 0;
  1405. err_pages:
  1406. while (i--)
  1407. page_cache_release(obj_priv->pages[i]);
  1408. drm_free_large(obj_priv->pages);
  1409. obj_priv->pages = NULL;
  1410. return PTR_ERR(page);
  1411. }
  1412. static void
  1413. i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
  1414. {
  1415. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1416. int page_count = obj->size / PAGE_SIZE;
  1417. int i;
  1418. BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
  1419. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1420. i915_gem_object_save_bit_17_swizzle(obj);
  1421. if (obj_priv->madv == I915_MADV_DONTNEED)
  1422. obj_priv->dirty = 0;
  1423. for (i = 0; i < page_count; i++) {
  1424. if (obj_priv->dirty)
  1425. set_page_dirty(obj_priv->pages[i]);
  1426. if (obj_priv->madv == I915_MADV_WILLNEED)
  1427. mark_page_accessed(obj_priv->pages[i]);
  1428. page_cache_release(obj_priv->pages[i]);
  1429. }
  1430. obj_priv->dirty = 0;
  1431. drm_free_large(obj_priv->pages);
  1432. obj_priv->pages = NULL;
  1433. }
  1434. static uint32_t
  1435. i915_gem_next_request_seqno(struct drm_device *dev,
  1436. struct intel_ring_buffer *ring)
  1437. {
  1438. drm_i915_private_t *dev_priv = dev->dev_private;
  1439. return ring->outstanding_lazy_request = dev_priv->next_seqno;
  1440. }
  1441. static void
  1442. i915_gem_object_move_to_active(struct drm_gem_object *obj,
  1443. struct intel_ring_buffer *ring)
  1444. {
  1445. struct drm_device *dev = obj->dev;
  1446. struct drm_i915_private *dev_priv = dev->dev_private;
  1447. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1448. uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
  1449. BUG_ON(ring == NULL);
  1450. obj_priv->ring = ring;
  1451. /* Add a reference if we're newly entering the active list. */
  1452. if (!obj_priv->active) {
  1453. drm_gem_object_reference(obj);
  1454. obj_priv->active = 1;
  1455. }
  1456. /* Move from whatever list we were on to the tail of execution. */
  1457. list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
  1458. list_move_tail(&obj_priv->ring_list, &ring->active_list);
  1459. obj_priv->last_rendering_seqno = seqno;
  1460. }
  1461. static void
  1462. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  1463. {
  1464. struct drm_device *dev = obj->dev;
  1465. drm_i915_private_t *dev_priv = dev->dev_private;
  1466. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1467. BUG_ON(!obj_priv->active);
  1468. list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
  1469. list_del_init(&obj_priv->ring_list);
  1470. obj_priv->last_rendering_seqno = 0;
  1471. }
  1472. /* Immediately discard the backing storage */
  1473. static void
  1474. i915_gem_object_truncate(struct drm_gem_object *obj)
  1475. {
  1476. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1477. struct inode *inode;
  1478. /* Our goal here is to return as much of the memory as
  1479. * is possible back to the system as we are called from OOM.
  1480. * To do this we must instruct the shmfs to drop all of its
  1481. * backing pages, *now*. Here we mirror the actions taken
  1482. * when by shmem_delete_inode() to release the backing store.
  1483. */
  1484. inode = obj->filp->f_path.dentry->d_inode;
  1485. truncate_inode_pages(inode->i_mapping, 0);
  1486. if (inode->i_op->truncate_range)
  1487. inode->i_op->truncate_range(inode, 0, (loff_t)-1);
  1488. obj_priv->madv = __I915_MADV_PURGED;
  1489. }
  1490. static inline int
  1491. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
  1492. {
  1493. return obj_priv->madv == I915_MADV_DONTNEED;
  1494. }
  1495. static void
  1496. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  1497. {
  1498. struct drm_device *dev = obj->dev;
  1499. drm_i915_private_t *dev_priv = dev->dev_private;
  1500. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1501. if (obj_priv->pin_count != 0)
  1502. list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
  1503. else
  1504. list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
  1505. list_del_init(&obj_priv->ring_list);
  1506. BUG_ON(!list_empty(&obj_priv->gpu_write_list));
  1507. obj_priv->last_rendering_seqno = 0;
  1508. obj_priv->ring = NULL;
  1509. if (obj_priv->active) {
  1510. obj_priv->active = 0;
  1511. drm_gem_object_unreference(obj);
  1512. }
  1513. WARN_ON(i915_verify_lists(dev));
  1514. }
  1515. static void
  1516. i915_gem_process_flushing_list(struct drm_device *dev,
  1517. uint32_t flush_domains,
  1518. struct intel_ring_buffer *ring)
  1519. {
  1520. drm_i915_private_t *dev_priv = dev->dev_private;
  1521. struct drm_i915_gem_object *obj_priv, *next;
  1522. list_for_each_entry_safe(obj_priv, next,
  1523. &ring->gpu_write_list,
  1524. gpu_write_list) {
  1525. struct drm_gem_object *obj = &obj_priv->base;
  1526. if (obj->write_domain & flush_domains) {
  1527. uint32_t old_write_domain = obj->write_domain;
  1528. obj->write_domain = 0;
  1529. list_del_init(&obj_priv->gpu_write_list);
  1530. i915_gem_object_move_to_active(obj, ring);
  1531. /* update the fence lru list */
  1532. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1533. struct drm_i915_fence_reg *reg =
  1534. &dev_priv->fence_regs[obj_priv->fence_reg];
  1535. list_move_tail(&reg->lru_list,
  1536. &dev_priv->mm.fence_list);
  1537. }
  1538. trace_i915_gem_object_change_domain(obj,
  1539. obj->read_domains,
  1540. old_write_domain);
  1541. }
  1542. }
  1543. }
  1544. int
  1545. i915_add_request(struct drm_device *dev,
  1546. struct drm_file *file,
  1547. struct drm_i915_gem_request *request,
  1548. struct intel_ring_buffer *ring)
  1549. {
  1550. drm_i915_private_t *dev_priv = dev->dev_private;
  1551. struct drm_i915_file_private *file_priv = NULL;
  1552. uint32_t seqno;
  1553. int was_empty;
  1554. int ret;
  1555. BUG_ON(request == NULL);
  1556. if (file != NULL)
  1557. file_priv = file->driver_priv;
  1558. ret = ring->add_request(ring, &seqno);
  1559. if (ret)
  1560. return ret;
  1561. ring->outstanding_lazy_request = false;
  1562. request->seqno = seqno;
  1563. request->ring = ring;
  1564. request->emitted_jiffies = jiffies;
  1565. was_empty = list_empty(&ring->request_list);
  1566. list_add_tail(&request->list, &ring->request_list);
  1567. if (file_priv) {
  1568. spin_lock(&file_priv->mm.lock);
  1569. request->file_priv = file_priv;
  1570. list_add_tail(&request->client_list,
  1571. &file_priv->mm.request_list);
  1572. spin_unlock(&file_priv->mm.lock);
  1573. }
  1574. if (!dev_priv->mm.suspended) {
  1575. mod_timer(&dev_priv->hangcheck_timer,
  1576. jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
  1577. if (was_empty)
  1578. queue_delayed_work(dev_priv->wq,
  1579. &dev_priv->mm.retire_work, HZ);
  1580. }
  1581. return 0;
  1582. }
  1583. /**
  1584. * Command execution barrier
  1585. *
  1586. * Ensures that all commands in the ring are finished
  1587. * before signalling the CPU
  1588. */
  1589. static void
  1590. i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
  1591. {
  1592. uint32_t flush_domains = 0;
  1593. /* The sampler always gets flushed on i965 (sigh) */
  1594. if (INTEL_INFO(dev)->gen >= 4)
  1595. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  1596. ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
  1597. }
  1598. static inline void
  1599. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1600. {
  1601. struct drm_i915_file_private *file_priv = request->file_priv;
  1602. if (!file_priv)
  1603. return;
  1604. spin_lock(&file_priv->mm.lock);
  1605. list_del(&request->client_list);
  1606. request->file_priv = NULL;
  1607. spin_unlock(&file_priv->mm.lock);
  1608. }
  1609. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1610. struct intel_ring_buffer *ring)
  1611. {
  1612. while (!list_empty(&ring->request_list)) {
  1613. struct drm_i915_gem_request *request;
  1614. request = list_first_entry(&ring->request_list,
  1615. struct drm_i915_gem_request,
  1616. list);
  1617. list_del(&request->list);
  1618. i915_gem_request_remove_from_client(request);
  1619. kfree(request);
  1620. }
  1621. while (!list_empty(&ring->active_list)) {
  1622. struct drm_i915_gem_object *obj_priv;
  1623. obj_priv = list_first_entry(&ring->active_list,
  1624. struct drm_i915_gem_object,
  1625. ring_list);
  1626. obj_priv->base.write_domain = 0;
  1627. list_del_init(&obj_priv->gpu_write_list);
  1628. i915_gem_object_move_to_inactive(&obj_priv->base);
  1629. }
  1630. }
  1631. void i915_gem_reset(struct drm_device *dev)
  1632. {
  1633. struct drm_i915_private *dev_priv = dev->dev_private;
  1634. struct drm_i915_gem_object *obj_priv;
  1635. int i;
  1636. i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
  1637. i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
  1638. i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
  1639. /* Remove anything from the flushing lists. The GPU cache is likely
  1640. * to be lost on reset along with the data, so simply move the
  1641. * lost bo to the inactive list.
  1642. */
  1643. while (!list_empty(&dev_priv->mm.flushing_list)) {
  1644. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  1645. struct drm_i915_gem_object,
  1646. mm_list);
  1647. obj_priv->base.write_domain = 0;
  1648. list_del_init(&obj_priv->gpu_write_list);
  1649. i915_gem_object_move_to_inactive(&obj_priv->base);
  1650. }
  1651. /* Move everything out of the GPU domains to ensure we do any
  1652. * necessary invalidation upon reuse.
  1653. */
  1654. list_for_each_entry(obj_priv,
  1655. &dev_priv->mm.inactive_list,
  1656. mm_list)
  1657. {
  1658. obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1659. }
  1660. /* The fence registers are invalidated so clear them out */
  1661. for (i = 0; i < 16; i++) {
  1662. struct drm_i915_fence_reg *reg;
  1663. reg = &dev_priv->fence_regs[i];
  1664. if (!reg->obj)
  1665. continue;
  1666. i915_gem_clear_fence_reg(reg->obj);
  1667. }
  1668. }
  1669. /**
  1670. * This function clears the request list as sequence numbers are passed.
  1671. */
  1672. static void
  1673. i915_gem_retire_requests_ring(struct drm_device *dev,
  1674. struct intel_ring_buffer *ring)
  1675. {
  1676. drm_i915_private_t *dev_priv = dev->dev_private;
  1677. uint32_t seqno;
  1678. if (!ring->status_page.page_addr ||
  1679. list_empty(&ring->request_list))
  1680. return;
  1681. WARN_ON(i915_verify_lists(dev));
  1682. seqno = ring->get_seqno(ring);
  1683. while (!list_empty(&ring->request_list)) {
  1684. struct drm_i915_gem_request *request;
  1685. request = list_first_entry(&ring->request_list,
  1686. struct drm_i915_gem_request,
  1687. list);
  1688. if (!i915_seqno_passed(seqno, request->seqno))
  1689. break;
  1690. trace_i915_gem_request_retire(dev, request->seqno);
  1691. list_del(&request->list);
  1692. i915_gem_request_remove_from_client(request);
  1693. kfree(request);
  1694. }
  1695. /* Move any buffers on the active list that are no longer referenced
  1696. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1697. */
  1698. while (!list_empty(&ring->active_list)) {
  1699. struct drm_gem_object *obj;
  1700. struct drm_i915_gem_object *obj_priv;
  1701. obj_priv = list_first_entry(&ring->active_list,
  1702. struct drm_i915_gem_object,
  1703. ring_list);
  1704. if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
  1705. break;
  1706. obj = &obj_priv->base;
  1707. if (obj->write_domain != 0)
  1708. i915_gem_object_move_to_flushing(obj);
  1709. else
  1710. i915_gem_object_move_to_inactive(obj);
  1711. }
  1712. if (unlikely (dev_priv->trace_irq_seqno &&
  1713. i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
  1714. ring->user_irq_put(ring);
  1715. dev_priv->trace_irq_seqno = 0;
  1716. }
  1717. WARN_ON(i915_verify_lists(dev));
  1718. }
  1719. void
  1720. i915_gem_retire_requests(struct drm_device *dev)
  1721. {
  1722. drm_i915_private_t *dev_priv = dev->dev_private;
  1723. if (!list_empty(&dev_priv->mm.deferred_free_list)) {
  1724. struct drm_i915_gem_object *obj_priv, *tmp;
  1725. /* We must be careful that during unbind() we do not
  1726. * accidentally infinitely recurse into retire requests.
  1727. * Currently:
  1728. * retire -> free -> unbind -> wait -> retire_ring
  1729. */
  1730. list_for_each_entry_safe(obj_priv, tmp,
  1731. &dev_priv->mm.deferred_free_list,
  1732. mm_list)
  1733. i915_gem_free_object_tail(&obj_priv->base);
  1734. }
  1735. i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
  1736. i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
  1737. i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
  1738. }
  1739. static void
  1740. i915_gem_retire_work_handler(struct work_struct *work)
  1741. {
  1742. drm_i915_private_t *dev_priv;
  1743. struct drm_device *dev;
  1744. dev_priv = container_of(work, drm_i915_private_t,
  1745. mm.retire_work.work);
  1746. dev = dev_priv->dev;
  1747. /* Come back later if the device is busy... */
  1748. if (!mutex_trylock(&dev->struct_mutex)) {
  1749. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1750. return;
  1751. }
  1752. i915_gem_retire_requests(dev);
  1753. if (!dev_priv->mm.suspended &&
  1754. (!list_empty(&dev_priv->render_ring.request_list) ||
  1755. !list_empty(&dev_priv->bsd_ring.request_list) ||
  1756. !list_empty(&dev_priv->blt_ring.request_list)))
  1757. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1758. mutex_unlock(&dev->struct_mutex);
  1759. }
  1760. int
  1761. i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
  1762. bool interruptible, struct intel_ring_buffer *ring)
  1763. {
  1764. drm_i915_private_t *dev_priv = dev->dev_private;
  1765. u32 ier;
  1766. int ret = 0;
  1767. BUG_ON(seqno == 0);
  1768. if (atomic_read(&dev_priv->mm.wedged))
  1769. return -EAGAIN;
  1770. if (seqno == ring->outstanding_lazy_request) {
  1771. struct drm_i915_gem_request *request;
  1772. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1773. if (request == NULL)
  1774. return -ENOMEM;
  1775. ret = i915_add_request(dev, NULL, request, ring);
  1776. if (ret) {
  1777. kfree(request);
  1778. return ret;
  1779. }
  1780. seqno = request->seqno;
  1781. }
  1782. if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
  1783. if (HAS_PCH_SPLIT(dev))
  1784. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1785. else
  1786. ier = I915_READ(IER);
  1787. if (!ier) {
  1788. DRM_ERROR("something (likely vbetool) disabled "
  1789. "interrupts, re-enabling\n");
  1790. i915_driver_irq_preinstall(dev);
  1791. i915_driver_irq_postinstall(dev);
  1792. }
  1793. trace_i915_gem_request_wait_begin(dev, seqno);
  1794. ring->waiting_seqno = seqno;
  1795. ring->user_irq_get(ring);
  1796. if (interruptible)
  1797. ret = wait_event_interruptible(ring->irq_queue,
  1798. i915_seqno_passed(ring->get_seqno(ring), seqno)
  1799. || atomic_read(&dev_priv->mm.wedged));
  1800. else
  1801. wait_event(ring->irq_queue,
  1802. i915_seqno_passed(ring->get_seqno(ring), seqno)
  1803. || atomic_read(&dev_priv->mm.wedged));
  1804. ring->user_irq_put(ring);
  1805. ring->waiting_seqno = 0;
  1806. trace_i915_gem_request_wait_end(dev, seqno);
  1807. }
  1808. if (atomic_read(&dev_priv->mm.wedged))
  1809. ret = -EAGAIN;
  1810. if (ret && ret != -ERESTARTSYS)
  1811. DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
  1812. __func__, ret, seqno, ring->get_seqno(ring),
  1813. dev_priv->next_seqno);
  1814. /* Directly dispatch request retiring. While we have the work queue
  1815. * to handle this, the waiter on a request often wants an associated
  1816. * buffer to have made it to the inactive list, and we would need
  1817. * a separate wait queue to handle that.
  1818. */
  1819. if (ret == 0)
  1820. i915_gem_retire_requests_ring(dev, ring);
  1821. return ret;
  1822. }
  1823. /**
  1824. * Waits for a sequence number to be signaled, and cleans up the
  1825. * request and object lists appropriately for that event.
  1826. */
  1827. static int
  1828. i915_wait_request(struct drm_device *dev, uint32_t seqno,
  1829. struct intel_ring_buffer *ring)
  1830. {
  1831. return i915_do_wait_request(dev, seqno, 1, ring);
  1832. }
  1833. static void
  1834. i915_gem_flush_ring(struct drm_device *dev,
  1835. struct drm_file *file_priv,
  1836. struct intel_ring_buffer *ring,
  1837. uint32_t invalidate_domains,
  1838. uint32_t flush_domains)
  1839. {
  1840. ring->flush(ring, invalidate_domains, flush_domains);
  1841. i915_gem_process_flushing_list(dev, flush_domains, ring);
  1842. }
  1843. static void
  1844. i915_gem_flush(struct drm_device *dev,
  1845. struct drm_file *file_priv,
  1846. uint32_t invalidate_domains,
  1847. uint32_t flush_domains,
  1848. uint32_t flush_rings)
  1849. {
  1850. drm_i915_private_t *dev_priv = dev->dev_private;
  1851. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1852. intel_gtt_chipset_flush();
  1853. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  1854. if (flush_rings & RING_RENDER)
  1855. i915_gem_flush_ring(dev, file_priv,
  1856. &dev_priv->render_ring,
  1857. invalidate_domains, flush_domains);
  1858. if (flush_rings & RING_BSD)
  1859. i915_gem_flush_ring(dev, file_priv,
  1860. &dev_priv->bsd_ring,
  1861. invalidate_domains, flush_domains);
  1862. if (flush_rings & RING_BLT)
  1863. i915_gem_flush_ring(dev, file_priv,
  1864. &dev_priv->blt_ring,
  1865. invalidate_domains, flush_domains);
  1866. }
  1867. }
  1868. /**
  1869. * Ensures that all rendering to the object has completed and the object is
  1870. * safe to unbind from the GTT or access from the CPU.
  1871. */
  1872. static int
  1873. i915_gem_object_wait_rendering(struct drm_gem_object *obj,
  1874. bool interruptible)
  1875. {
  1876. struct drm_device *dev = obj->dev;
  1877. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1878. int ret;
  1879. /* This function only exists to support waiting for existing rendering,
  1880. * not for emitting required flushes.
  1881. */
  1882. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1883. /* If there is rendering queued on the buffer being evicted, wait for
  1884. * it.
  1885. */
  1886. if (obj_priv->active) {
  1887. ret = i915_do_wait_request(dev,
  1888. obj_priv->last_rendering_seqno,
  1889. interruptible,
  1890. obj_priv->ring);
  1891. if (ret)
  1892. return ret;
  1893. }
  1894. return 0;
  1895. }
  1896. /**
  1897. * Unbinds an object from the GTT aperture.
  1898. */
  1899. int
  1900. i915_gem_object_unbind(struct drm_gem_object *obj)
  1901. {
  1902. struct drm_device *dev = obj->dev;
  1903. struct drm_i915_private *dev_priv = dev->dev_private;
  1904. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1905. int ret = 0;
  1906. if (obj_priv->gtt_space == NULL)
  1907. return 0;
  1908. if (obj_priv->pin_count != 0) {
  1909. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1910. return -EINVAL;
  1911. }
  1912. /* blow away mappings if mapped through GTT */
  1913. i915_gem_release_mmap(obj);
  1914. /* Move the object to the CPU domain to ensure that
  1915. * any possible CPU writes while it's not in the GTT
  1916. * are flushed when we go to remap it. This will
  1917. * also ensure that all pending GPU writes are finished
  1918. * before we unbind.
  1919. */
  1920. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1921. if (ret == -ERESTARTSYS)
  1922. return ret;
  1923. /* Continue on if we fail due to EIO, the GPU is hung so we
  1924. * should be safe and we need to cleanup or else we might
  1925. * cause memory corruption through use-after-free.
  1926. */
  1927. if (ret) {
  1928. i915_gem_clflush_object(obj);
  1929. obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
  1930. }
  1931. /* release the fence reg _after_ flushing */
  1932. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1933. i915_gem_clear_fence_reg(obj);
  1934. drm_unbind_agp(obj_priv->agp_mem);
  1935. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1936. i915_gem_object_put_pages_gtt(obj);
  1937. i915_gem_info_remove_gtt(dev_priv, obj_priv);
  1938. list_del_init(&obj_priv->mm_list);
  1939. /* Avoid an unnecessary call to unbind on rebind. */
  1940. obj_priv->map_and_fenceable = true;
  1941. drm_mm_put_block(obj_priv->gtt_space);
  1942. obj_priv->gtt_space = NULL;
  1943. obj_priv->gtt_offset = 0;
  1944. if (i915_gem_object_is_purgeable(obj_priv))
  1945. i915_gem_object_truncate(obj);
  1946. trace_i915_gem_object_unbind(obj);
  1947. return ret;
  1948. }
  1949. static int i915_ring_idle(struct drm_device *dev,
  1950. struct intel_ring_buffer *ring)
  1951. {
  1952. if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
  1953. return 0;
  1954. i915_gem_flush_ring(dev, NULL, ring,
  1955. I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1956. return i915_wait_request(dev,
  1957. i915_gem_next_request_seqno(dev, ring),
  1958. ring);
  1959. }
  1960. int
  1961. i915_gpu_idle(struct drm_device *dev)
  1962. {
  1963. drm_i915_private_t *dev_priv = dev->dev_private;
  1964. bool lists_empty;
  1965. int ret;
  1966. lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
  1967. list_empty(&dev_priv->mm.active_list));
  1968. if (lists_empty)
  1969. return 0;
  1970. /* Flush everything onto the inactive list. */
  1971. ret = i915_ring_idle(dev, &dev_priv->render_ring);
  1972. if (ret)
  1973. return ret;
  1974. ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
  1975. if (ret)
  1976. return ret;
  1977. ret = i915_ring_idle(dev, &dev_priv->blt_ring);
  1978. if (ret)
  1979. return ret;
  1980. return 0;
  1981. }
  1982. static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
  1983. {
  1984. struct drm_device *dev = obj->dev;
  1985. drm_i915_private_t *dev_priv = dev->dev_private;
  1986. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1987. u32 size = i915_gem_get_gtt_size(obj_priv);
  1988. int regnum = obj_priv->fence_reg;
  1989. uint64_t val;
  1990. val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
  1991. 0xfffff000) << 32;
  1992. val |= obj_priv->gtt_offset & 0xfffff000;
  1993. val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
  1994. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1995. if (obj_priv->tiling_mode == I915_TILING_Y)
  1996. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1997. val |= I965_FENCE_REG_VALID;
  1998. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
  1999. }
  2000. static void i965_write_fence_reg(struct drm_gem_object *obj)
  2001. {
  2002. struct drm_device *dev = obj->dev;
  2003. drm_i915_private_t *dev_priv = dev->dev_private;
  2004. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2005. u32 size = i915_gem_get_gtt_size(obj_priv);
  2006. int regnum = obj_priv->fence_reg;
  2007. uint64_t val;
  2008. val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
  2009. 0xfffff000) << 32;
  2010. val |= obj_priv->gtt_offset & 0xfffff000;
  2011. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  2012. if (obj_priv->tiling_mode == I915_TILING_Y)
  2013. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2014. val |= I965_FENCE_REG_VALID;
  2015. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  2016. }
  2017. static void i915_write_fence_reg(struct drm_gem_object *obj)
  2018. {
  2019. struct drm_device *dev = obj->dev;
  2020. drm_i915_private_t *dev_priv = dev->dev_private;
  2021. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2022. u32 size = i915_gem_get_gtt_size(obj_priv);
  2023. uint32_t fence_reg, val, pitch_val;
  2024. int tile_width;
  2025. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  2026. (obj_priv->gtt_offset & (size - 1))) {
  2027. WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
  2028. __func__, obj_priv->gtt_offset, obj_priv->map_and_fenceable, size,
  2029. obj_priv->gtt_space->start, obj_priv->gtt_space->size);
  2030. return;
  2031. }
  2032. if (obj_priv->tiling_mode == I915_TILING_Y &&
  2033. HAS_128_BYTE_Y_TILING(dev))
  2034. tile_width = 128;
  2035. else
  2036. tile_width = 512;
  2037. /* Note: pitch better be a power of two tile widths */
  2038. pitch_val = obj_priv->stride / tile_width;
  2039. pitch_val = ffs(pitch_val) - 1;
  2040. if (obj_priv->tiling_mode == I915_TILING_Y &&
  2041. HAS_128_BYTE_Y_TILING(dev))
  2042. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  2043. else
  2044. WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
  2045. val = obj_priv->gtt_offset;
  2046. if (obj_priv->tiling_mode == I915_TILING_Y)
  2047. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2048. val |= I915_FENCE_SIZE_BITS(size);
  2049. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2050. val |= I830_FENCE_REG_VALID;
  2051. fence_reg = obj_priv->fence_reg;
  2052. if (fence_reg < 8)
  2053. fence_reg = FENCE_REG_830_0 + fence_reg * 4;
  2054. else
  2055. fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
  2056. I915_WRITE(fence_reg, val);
  2057. }
  2058. static void i830_write_fence_reg(struct drm_gem_object *obj)
  2059. {
  2060. struct drm_device *dev = obj->dev;
  2061. drm_i915_private_t *dev_priv = dev->dev_private;
  2062. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2063. u32 size = i915_gem_get_gtt_size(obj_priv);
  2064. int regnum = obj_priv->fence_reg;
  2065. uint32_t val;
  2066. uint32_t pitch_val;
  2067. uint32_t fence_size_bits;
  2068. if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
  2069. (obj_priv->gtt_offset & (obj->size - 1))) {
  2070. WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
  2071. __func__, obj_priv->gtt_offset);
  2072. return;
  2073. }
  2074. pitch_val = obj_priv->stride / 128;
  2075. pitch_val = ffs(pitch_val) - 1;
  2076. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  2077. val = obj_priv->gtt_offset;
  2078. if (obj_priv->tiling_mode == I915_TILING_Y)
  2079. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2080. fence_size_bits = I830_FENCE_SIZE_BITS(size);
  2081. WARN_ON(fence_size_bits & ~0x00000f00);
  2082. val |= fence_size_bits;
  2083. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2084. val |= I830_FENCE_REG_VALID;
  2085. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  2086. }
  2087. static int i915_find_fence_reg(struct drm_device *dev,
  2088. bool interruptible)
  2089. {
  2090. struct drm_i915_private *dev_priv = dev->dev_private;
  2091. struct drm_i915_fence_reg *reg;
  2092. struct drm_i915_gem_object *obj_priv = NULL;
  2093. int i, avail, ret;
  2094. /* First try to find a free reg */
  2095. avail = 0;
  2096. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2097. reg = &dev_priv->fence_regs[i];
  2098. if (!reg->obj)
  2099. return i;
  2100. obj_priv = to_intel_bo(reg->obj);
  2101. if (!obj_priv->pin_count)
  2102. avail++;
  2103. }
  2104. if (avail == 0)
  2105. return -ENOSPC;
  2106. /* None available, try to steal one or wait for a user to finish */
  2107. avail = I915_FENCE_REG_NONE;
  2108. list_for_each_entry(reg, &dev_priv->mm.fence_list,
  2109. lru_list) {
  2110. obj_priv = to_intel_bo(reg->obj);
  2111. if (obj_priv->pin_count)
  2112. continue;
  2113. /* found one! */
  2114. avail = obj_priv->fence_reg;
  2115. break;
  2116. }
  2117. BUG_ON(avail == I915_FENCE_REG_NONE);
  2118. /* We only have a reference on obj from the active list. put_fence_reg
  2119. * might drop that one, causing a use-after-free in it. So hold a
  2120. * private reference to obj like the other callers of put_fence_reg
  2121. * (set_tiling ioctl) do. */
  2122. drm_gem_object_reference(&obj_priv->base);
  2123. ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
  2124. drm_gem_object_unreference(&obj_priv->base);
  2125. if (ret != 0)
  2126. return ret;
  2127. return avail;
  2128. }
  2129. /**
  2130. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  2131. * @obj: object to map through a fence reg
  2132. *
  2133. * When mapping objects through the GTT, userspace wants to be able to write
  2134. * to them without having to worry about swizzling if the object is tiled.
  2135. *
  2136. * This function walks the fence regs looking for a free one for @obj,
  2137. * stealing one if it can't find any.
  2138. *
  2139. * It then sets up the reg based on the object's properties: address, pitch
  2140. * and tiling format.
  2141. */
  2142. int
  2143. i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
  2144. bool interruptible)
  2145. {
  2146. struct drm_device *dev = obj->dev;
  2147. struct drm_i915_private *dev_priv = dev->dev_private;
  2148. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2149. struct drm_i915_fence_reg *reg = NULL;
  2150. int ret;
  2151. /* Just update our place in the LRU if our fence is getting used. */
  2152. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  2153. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2154. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2155. return 0;
  2156. }
  2157. switch (obj_priv->tiling_mode) {
  2158. case I915_TILING_NONE:
  2159. WARN(1, "allocating a fence for non-tiled object?\n");
  2160. break;
  2161. case I915_TILING_X:
  2162. if (!obj_priv->stride)
  2163. return -EINVAL;
  2164. WARN((obj_priv->stride & (512 - 1)),
  2165. "object 0x%08x is X tiled but has non-512B pitch\n",
  2166. obj_priv->gtt_offset);
  2167. break;
  2168. case I915_TILING_Y:
  2169. if (!obj_priv->stride)
  2170. return -EINVAL;
  2171. WARN((obj_priv->stride & (128 - 1)),
  2172. "object 0x%08x is Y tiled but has non-128B pitch\n",
  2173. obj_priv->gtt_offset);
  2174. break;
  2175. }
  2176. ret = i915_find_fence_reg(dev, interruptible);
  2177. if (ret < 0)
  2178. return ret;
  2179. obj_priv->fence_reg = ret;
  2180. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2181. list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2182. reg->obj = obj;
  2183. switch (INTEL_INFO(dev)->gen) {
  2184. case 6:
  2185. sandybridge_write_fence_reg(obj);
  2186. break;
  2187. case 5:
  2188. case 4:
  2189. i965_write_fence_reg(obj);
  2190. break;
  2191. case 3:
  2192. i915_write_fence_reg(obj);
  2193. break;
  2194. case 2:
  2195. i830_write_fence_reg(obj);
  2196. break;
  2197. }
  2198. trace_i915_gem_object_get_fence(obj,
  2199. obj_priv->fence_reg,
  2200. obj_priv->tiling_mode);
  2201. return 0;
  2202. }
  2203. /**
  2204. * i915_gem_clear_fence_reg - clear out fence register info
  2205. * @obj: object to clear
  2206. *
  2207. * Zeroes out the fence register itself and clears out the associated
  2208. * data structures in dev_priv and obj_priv.
  2209. */
  2210. static void
  2211. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  2212. {
  2213. struct drm_device *dev = obj->dev;
  2214. drm_i915_private_t *dev_priv = dev->dev_private;
  2215. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2216. struct drm_i915_fence_reg *reg =
  2217. &dev_priv->fence_regs[obj_priv->fence_reg];
  2218. uint32_t fence_reg;
  2219. switch (INTEL_INFO(dev)->gen) {
  2220. case 6:
  2221. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
  2222. (obj_priv->fence_reg * 8), 0);
  2223. break;
  2224. case 5:
  2225. case 4:
  2226. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  2227. break;
  2228. case 3:
  2229. if (obj_priv->fence_reg >= 8)
  2230. fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
  2231. else
  2232. case 2:
  2233. fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
  2234. I915_WRITE(fence_reg, 0);
  2235. break;
  2236. }
  2237. reg->obj = NULL;
  2238. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2239. list_del_init(&reg->lru_list);
  2240. }
  2241. /**
  2242. * i915_gem_object_put_fence_reg - waits on outstanding fenced access
  2243. * to the buffer to finish, and then resets the fence register.
  2244. * @obj: tiled object holding a fence register.
  2245. * @bool: whether the wait upon the fence is interruptible
  2246. *
  2247. * Zeroes out the fence register itself and clears out the associated
  2248. * data structures in dev_priv and obj_priv.
  2249. */
  2250. int
  2251. i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
  2252. bool interruptible)
  2253. {
  2254. struct drm_device *dev = obj->dev;
  2255. struct drm_i915_private *dev_priv = dev->dev_private;
  2256. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2257. struct drm_i915_fence_reg *reg;
  2258. if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
  2259. return 0;
  2260. /* If we've changed tiling, GTT-mappings of the object
  2261. * need to re-fault to ensure that the correct fence register
  2262. * setup is in place.
  2263. */
  2264. i915_gem_release_mmap(obj);
  2265. /* On the i915, GPU access to tiled buffers is via a fence,
  2266. * therefore we must wait for any outstanding access to complete
  2267. * before clearing the fence.
  2268. */
  2269. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2270. if (reg->gpu) {
  2271. int ret;
  2272. ret = i915_gem_object_flush_gpu_write_domain(obj, true);
  2273. if (ret)
  2274. return ret;
  2275. ret = i915_gem_object_wait_rendering(obj, interruptible);
  2276. if (ret)
  2277. return ret;
  2278. reg->gpu = false;
  2279. }
  2280. i915_gem_object_flush_gtt_write_domain(obj);
  2281. i915_gem_clear_fence_reg(obj);
  2282. return 0;
  2283. }
  2284. /**
  2285. * Finds free space in the GTT aperture and binds the object there.
  2286. */
  2287. static int
  2288. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  2289. unsigned alignment,
  2290. bool map_and_fenceable)
  2291. {
  2292. struct drm_device *dev = obj->dev;
  2293. drm_i915_private_t *dev_priv = dev->dev_private;
  2294. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2295. struct drm_mm_node *free_space;
  2296. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2297. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2298. bool mappable, fenceable;
  2299. int ret;
  2300. if (obj_priv->madv != I915_MADV_WILLNEED) {
  2301. DRM_ERROR("Attempting to bind a purgeable object\n");
  2302. return -EINVAL;
  2303. }
  2304. fence_size = i915_gem_get_gtt_size(obj_priv);
  2305. fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
  2306. unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj_priv);
  2307. if (alignment == 0)
  2308. alignment = map_and_fenceable ? fence_alignment :
  2309. unfenced_alignment;
  2310. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2311. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2312. return -EINVAL;
  2313. }
  2314. size = map_and_fenceable ? fence_size : obj->size;
  2315. /* If the object is bigger than the entire aperture, reject it early
  2316. * before evicting everything in a vain attempt to find space.
  2317. */
  2318. if (obj->size >
  2319. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2320. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2321. return -E2BIG;
  2322. }
  2323. search_free:
  2324. if (map_and_fenceable)
  2325. free_space =
  2326. drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
  2327. size, alignment, 0,
  2328. dev_priv->mm.gtt_mappable_end,
  2329. 0);
  2330. else
  2331. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2332. size, alignment, 0);
  2333. if (free_space != NULL) {
  2334. if (map_and_fenceable)
  2335. obj_priv->gtt_space =
  2336. drm_mm_get_block_range_generic(free_space,
  2337. size, alignment, 0,
  2338. dev_priv->mm.gtt_mappable_end,
  2339. 0);
  2340. else
  2341. obj_priv->gtt_space =
  2342. drm_mm_get_block(free_space, size, alignment);
  2343. }
  2344. if (obj_priv->gtt_space == NULL) {
  2345. /* If the gtt is empty and we're still having trouble
  2346. * fitting our object in, we're out of memory.
  2347. */
  2348. ret = i915_gem_evict_something(dev, size, alignment,
  2349. map_and_fenceable);
  2350. if (ret)
  2351. return ret;
  2352. goto search_free;
  2353. }
  2354. ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
  2355. if (ret) {
  2356. drm_mm_put_block(obj_priv->gtt_space);
  2357. obj_priv->gtt_space = NULL;
  2358. if (ret == -ENOMEM) {
  2359. /* first try to clear up some space from the GTT */
  2360. ret = i915_gem_evict_something(dev, size,
  2361. alignment,
  2362. map_and_fenceable);
  2363. if (ret) {
  2364. /* now try to shrink everyone else */
  2365. if (gfpmask) {
  2366. gfpmask = 0;
  2367. goto search_free;
  2368. }
  2369. return ret;
  2370. }
  2371. goto search_free;
  2372. }
  2373. return ret;
  2374. }
  2375. /* Create an AGP memory structure pointing at our pages, and bind it
  2376. * into the GTT.
  2377. */
  2378. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  2379. obj_priv->pages,
  2380. obj->size >> PAGE_SHIFT,
  2381. obj_priv->gtt_space->start,
  2382. obj_priv->agp_type);
  2383. if (obj_priv->agp_mem == NULL) {
  2384. i915_gem_object_put_pages_gtt(obj);
  2385. drm_mm_put_block(obj_priv->gtt_space);
  2386. obj_priv->gtt_space = NULL;
  2387. ret = i915_gem_evict_something(dev, size,
  2388. alignment, map_and_fenceable);
  2389. if (ret)
  2390. return ret;
  2391. goto search_free;
  2392. }
  2393. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  2394. /* keep track of bounds object by adding it to the inactive list */
  2395. list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
  2396. i915_gem_info_add_gtt(dev_priv, obj_priv);
  2397. /* Assert that the object is not currently in any GPU domain. As it
  2398. * wasn't in the GTT, there shouldn't be any way it could have been in
  2399. * a GPU cache
  2400. */
  2401. BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
  2402. BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
  2403. trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, map_and_fenceable);
  2404. fenceable =
  2405. obj_priv->gtt_space->size == fence_size &&
  2406. (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
  2407. mappable =
  2408. obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
  2409. obj_priv->map_and_fenceable = mappable && fenceable;
  2410. return 0;
  2411. }
  2412. void
  2413. i915_gem_clflush_object(struct drm_gem_object *obj)
  2414. {
  2415. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2416. /* If we don't have a page list set up, then we're not pinned
  2417. * to GPU, and we can ignore the cache flush because it'll happen
  2418. * again at bind time.
  2419. */
  2420. if (obj_priv->pages == NULL)
  2421. return;
  2422. trace_i915_gem_object_clflush(obj);
  2423. drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
  2424. }
  2425. /** Flushes any GPU write domain for the object if it's dirty. */
  2426. static int
  2427. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
  2428. bool pipelined)
  2429. {
  2430. struct drm_device *dev = obj->dev;
  2431. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2432. return 0;
  2433. /* Queue the GPU write cache flushing we need. */
  2434. i915_gem_flush_ring(dev, NULL,
  2435. to_intel_bo(obj)->ring,
  2436. 0, obj->write_domain);
  2437. BUG_ON(obj->write_domain);
  2438. if (pipelined)
  2439. return 0;
  2440. return i915_gem_object_wait_rendering(obj, true);
  2441. }
  2442. /** Flushes the GTT write domain for the object if it's dirty. */
  2443. static void
  2444. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  2445. {
  2446. uint32_t old_write_domain;
  2447. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  2448. return;
  2449. /* No actual flushing is required for the GTT write domain. Writes
  2450. * to it immediately go to main memory as far as we know, so there's
  2451. * no chipset flush. It also doesn't land in render cache.
  2452. */
  2453. i915_gem_release_mmap(obj);
  2454. old_write_domain = obj->write_domain;
  2455. obj->write_domain = 0;
  2456. trace_i915_gem_object_change_domain(obj,
  2457. obj->read_domains,
  2458. old_write_domain);
  2459. }
  2460. /** Flushes the CPU write domain for the object if it's dirty. */
  2461. static void
  2462. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  2463. {
  2464. uint32_t old_write_domain;
  2465. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  2466. return;
  2467. i915_gem_clflush_object(obj);
  2468. intel_gtt_chipset_flush();
  2469. old_write_domain = obj->write_domain;
  2470. obj->write_domain = 0;
  2471. trace_i915_gem_object_change_domain(obj,
  2472. obj->read_domains,
  2473. old_write_domain);
  2474. }
  2475. /**
  2476. * Moves a single object to the GTT read, and possibly write domain.
  2477. *
  2478. * This function returns when the move is complete, including waiting on
  2479. * flushes to occur.
  2480. */
  2481. int
  2482. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  2483. {
  2484. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2485. uint32_t old_write_domain, old_read_domains;
  2486. int ret;
  2487. /* Not valid to be called on unbound objects. */
  2488. if (obj_priv->gtt_space == NULL)
  2489. return -EINVAL;
  2490. ret = i915_gem_object_flush_gpu_write_domain(obj, false);
  2491. if (ret != 0)
  2492. return ret;
  2493. i915_gem_object_flush_cpu_write_domain(obj);
  2494. if (write) {
  2495. ret = i915_gem_object_wait_rendering(obj, true);
  2496. if (ret)
  2497. return ret;
  2498. }
  2499. old_write_domain = obj->write_domain;
  2500. old_read_domains = obj->read_domains;
  2501. /* It should now be out of any other write domains, and we can update
  2502. * the domain values for our changes.
  2503. */
  2504. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2505. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2506. if (write) {
  2507. obj->read_domains = I915_GEM_DOMAIN_GTT;
  2508. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2509. obj_priv->dirty = 1;
  2510. }
  2511. trace_i915_gem_object_change_domain(obj,
  2512. old_read_domains,
  2513. old_write_domain);
  2514. return 0;
  2515. }
  2516. /*
  2517. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2518. * wait, as in modesetting process we're not supposed to be interrupted.
  2519. */
  2520. int
  2521. i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
  2522. bool pipelined)
  2523. {
  2524. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2525. uint32_t old_read_domains;
  2526. int ret;
  2527. /* Not valid to be called on unbound objects. */
  2528. if (obj_priv->gtt_space == NULL)
  2529. return -EINVAL;
  2530. ret = i915_gem_object_flush_gpu_write_domain(obj, true);
  2531. if (ret)
  2532. return ret;
  2533. /* Currently, we are always called from an non-interruptible context. */
  2534. if (!pipelined) {
  2535. ret = i915_gem_object_wait_rendering(obj, false);
  2536. if (ret)
  2537. return ret;
  2538. }
  2539. i915_gem_object_flush_cpu_write_domain(obj);
  2540. old_read_domains = obj->read_domains;
  2541. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2542. trace_i915_gem_object_change_domain(obj,
  2543. old_read_domains,
  2544. obj->write_domain);
  2545. return 0;
  2546. }
  2547. int
  2548. i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
  2549. bool interruptible)
  2550. {
  2551. if (!obj->active)
  2552. return 0;
  2553. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
  2554. i915_gem_flush_ring(obj->base.dev, NULL, obj->ring,
  2555. 0, obj->base.write_domain);
  2556. return i915_gem_object_wait_rendering(&obj->base, interruptible);
  2557. }
  2558. /**
  2559. * Moves a single object to the CPU read, and possibly write domain.
  2560. *
  2561. * This function returns when the move is complete, including waiting on
  2562. * flushes to occur.
  2563. */
  2564. static int
  2565. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  2566. {
  2567. uint32_t old_write_domain, old_read_domains;
  2568. int ret;
  2569. ret = i915_gem_object_flush_gpu_write_domain(obj, false);
  2570. if (ret != 0)
  2571. return ret;
  2572. i915_gem_object_flush_gtt_write_domain(obj);
  2573. /* If we have a partially-valid cache of the object in the CPU,
  2574. * finish invalidating it and free the per-page flags.
  2575. */
  2576. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2577. if (write) {
  2578. ret = i915_gem_object_wait_rendering(obj, true);
  2579. if (ret)
  2580. return ret;
  2581. }
  2582. old_write_domain = obj->write_domain;
  2583. old_read_domains = obj->read_domains;
  2584. /* Flush the CPU cache if it's still invalid. */
  2585. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2586. i915_gem_clflush_object(obj);
  2587. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2588. }
  2589. /* It should now be out of any other write domains, and we can update
  2590. * the domain values for our changes.
  2591. */
  2592. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2593. /* If we're writing through the CPU, then the GPU read domains will
  2594. * need to be invalidated at next use.
  2595. */
  2596. if (write) {
  2597. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2598. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2599. }
  2600. trace_i915_gem_object_change_domain(obj,
  2601. old_read_domains,
  2602. old_write_domain);
  2603. return 0;
  2604. }
  2605. /*
  2606. * Set the next domain for the specified object. This
  2607. * may not actually perform the necessary flushing/invaliding though,
  2608. * as that may want to be batched with other set_domain operations
  2609. *
  2610. * This is (we hope) the only really tricky part of gem. The goal
  2611. * is fairly simple -- track which caches hold bits of the object
  2612. * and make sure they remain coherent. A few concrete examples may
  2613. * help to explain how it works. For shorthand, we use the notation
  2614. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  2615. * a pair of read and write domain masks.
  2616. *
  2617. * Case 1: the batch buffer
  2618. *
  2619. * 1. Allocated
  2620. * 2. Written by CPU
  2621. * 3. Mapped to GTT
  2622. * 4. Read by GPU
  2623. * 5. Unmapped from GTT
  2624. * 6. Freed
  2625. *
  2626. * Let's take these a step at a time
  2627. *
  2628. * 1. Allocated
  2629. * Pages allocated from the kernel may still have
  2630. * cache contents, so we set them to (CPU, CPU) always.
  2631. * 2. Written by CPU (using pwrite)
  2632. * The pwrite function calls set_domain (CPU, CPU) and
  2633. * this function does nothing (as nothing changes)
  2634. * 3. Mapped by GTT
  2635. * This function asserts that the object is not
  2636. * currently in any GPU-based read or write domains
  2637. * 4. Read by GPU
  2638. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  2639. * As write_domain is zero, this function adds in the
  2640. * current read domains (CPU+COMMAND, 0).
  2641. * flush_domains is set to CPU.
  2642. * invalidate_domains is set to COMMAND
  2643. * clflush is run to get data out of the CPU caches
  2644. * then i915_dev_set_domain calls i915_gem_flush to
  2645. * emit an MI_FLUSH and drm_agp_chipset_flush
  2646. * 5. Unmapped from GTT
  2647. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  2648. * flush_domains and invalidate_domains end up both zero
  2649. * so no flushing/invalidating happens
  2650. * 6. Freed
  2651. * yay, done
  2652. *
  2653. * Case 2: The shared render buffer
  2654. *
  2655. * 1. Allocated
  2656. * 2. Mapped to GTT
  2657. * 3. Read/written by GPU
  2658. * 4. set_domain to (CPU,CPU)
  2659. * 5. Read/written by CPU
  2660. * 6. Read/written by GPU
  2661. *
  2662. * 1. Allocated
  2663. * Same as last example, (CPU, CPU)
  2664. * 2. Mapped to GTT
  2665. * Nothing changes (assertions find that it is not in the GPU)
  2666. * 3. Read/written by GPU
  2667. * execbuffer calls set_domain (RENDER, RENDER)
  2668. * flush_domains gets CPU
  2669. * invalidate_domains gets GPU
  2670. * clflush (obj)
  2671. * MI_FLUSH and drm_agp_chipset_flush
  2672. * 4. set_domain (CPU, CPU)
  2673. * flush_domains gets GPU
  2674. * invalidate_domains gets CPU
  2675. * wait_rendering (obj) to make sure all drawing is complete.
  2676. * This will include an MI_FLUSH to get the data from GPU
  2677. * to memory
  2678. * clflush (obj) to invalidate the CPU cache
  2679. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  2680. * 5. Read/written by CPU
  2681. * cache lines are loaded and dirtied
  2682. * 6. Read written by GPU
  2683. * Same as last GPU access
  2684. *
  2685. * Case 3: The constant buffer
  2686. *
  2687. * 1. Allocated
  2688. * 2. Written by CPU
  2689. * 3. Read by GPU
  2690. * 4. Updated (written) by CPU again
  2691. * 5. Read by GPU
  2692. *
  2693. * 1. Allocated
  2694. * (CPU, CPU)
  2695. * 2. Written by CPU
  2696. * (CPU, CPU)
  2697. * 3. Read by GPU
  2698. * (CPU+RENDER, 0)
  2699. * flush_domains = CPU
  2700. * invalidate_domains = RENDER
  2701. * clflush (obj)
  2702. * MI_FLUSH
  2703. * drm_agp_chipset_flush
  2704. * 4. Updated (written) by CPU again
  2705. * (CPU, CPU)
  2706. * flush_domains = 0 (no previous write domain)
  2707. * invalidate_domains = 0 (no new read domains)
  2708. * 5. Read by GPU
  2709. * (CPU+RENDER, 0)
  2710. * flush_domains = CPU
  2711. * invalidate_domains = RENDER
  2712. * clflush (obj)
  2713. * MI_FLUSH
  2714. * drm_agp_chipset_flush
  2715. */
  2716. static void
  2717. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  2718. struct intel_ring_buffer *ring,
  2719. struct change_domains *cd)
  2720. {
  2721. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2722. uint32_t invalidate_domains = 0;
  2723. uint32_t flush_domains = 0;
  2724. /*
  2725. * If the object isn't moving to a new write domain,
  2726. * let the object stay in multiple read domains
  2727. */
  2728. if (obj->pending_write_domain == 0)
  2729. obj->pending_read_domains |= obj->read_domains;
  2730. /*
  2731. * Flush the current write domain if
  2732. * the new read domains don't match. Invalidate
  2733. * any read domains which differ from the old
  2734. * write domain
  2735. */
  2736. if (obj->write_domain &&
  2737. (obj->write_domain != obj->pending_read_domains ||
  2738. obj_priv->ring != ring)) {
  2739. flush_domains |= obj->write_domain;
  2740. invalidate_domains |=
  2741. obj->pending_read_domains & ~obj->write_domain;
  2742. }
  2743. /*
  2744. * Invalidate any read caches which may have
  2745. * stale data. That is, any new read domains.
  2746. */
  2747. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  2748. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
  2749. i915_gem_clflush_object(obj);
  2750. /* blow away mappings if mapped through GTT */
  2751. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
  2752. i915_gem_release_mmap(obj);
  2753. /* The actual obj->write_domain will be updated with
  2754. * pending_write_domain after we emit the accumulated flush for all
  2755. * of our domain changes in execbuffers (which clears objects'
  2756. * write_domains). So if we have a current write domain that we
  2757. * aren't changing, set pending_write_domain to that.
  2758. */
  2759. if (flush_domains == 0 && obj->pending_write_domain == 0)
  2760. obj->pending_write_domain = obj->write_domain;
  2761. cd->invalidate_domains |= invalidate_domains;
  2762. cd->flush_domains |= flush_domains;
  2763. if (flush_domains & I915_GEM_GPU_DOMAINS)
  2764. cd->flush_rings |= obj_priv->ring->id;
  2765. if (invalidate_domains & I915_GEM_GPU_DOMAINS)
  2766. cd->flush_rings |= ring->id;
  2767. }
  2768. /**
  2769. * Moves the object from a partially CPU read to a full one.
  2770. *
  2771. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2772. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2773. */
  2774. static void
  2775. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  2776. {
  2777. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2778. if (!obj_priv->page_cpu_valid)
  2779. return;
  2780. /* If we're partially in the CPU read domain, finish moving it in.
  2781. */
  2782. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  2783. int i;
  2784. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  2785. if (obj_priv->page_cpu_valid[i])
  2786. continue;
  2787. drm_clflush_pages(obj_priv->pages + i, 1);
  2788. }
  2789. }
  2790. /* Free the page_cpu_valid mappings which are now stale, whether
  2791. * or not we've got I915_GEM_DOMAIN_CPU.
  2792. */
  2793. kfree(obj_priv->page_cpu_valid);
  2794. obj_priv->page_cpu_valid = NULL;
  2795. }
  2796. /**
  2797. * Set the CPU read domain on a range of the object.
  2798. *
  2799. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2800. * not entirely valid. The page_cpu_valid member of the object flags which
  2801. * pages have been flushed, and will be respected by
  2802. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2803. * of the whole object.
  2804. *
  2805. * This function returns when the move is complete, including waiting on
  2806. * flushes to occur.
  2807. */
  2808. static int
  2809. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  2810. uint64_t offset, uint64_t size)
  2811. {
  2812. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2813. uint32_t old_read_domains;
  2814. int i, ret;
  2815. if (offset == 0 && size == obj->size)
  2816. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2817. ret = i915_gem_object_flush_gpu_write_domain(obj, false);
  2818. if (ret != 0)
  2819. return ret;
  2820. i915_gem_object_flush_gtt_write_domain(obj);
  2821. /* If we're already fully in the CPU read domain, we're done. */
  2822. if (obj_priv->page_cpu_valid == NULL &&
  2823. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2824. return 0;
  2825. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2826. * newly adding I915_GEM_DOMAIN_CPU
  2827. */
  2828. if (obj_priv->page_cpu_valid == NULL) {
  2829. obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
  2830. GFP_KERNEL);
  2831. if (obj_priv->page_cpu_valid == NULL)
  2832. return -ENOMEM;
  2833. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2834. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  2835. /* Flush the cache on any pages that are still invalid from the CPU's
  2836. * perspective.
  2837. */
  2838. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2839. i++) {
  2840. if (obj_priv->page_cpu_valid[i])
  2841. continue;
  2842. drm_clflush_pages(obj_priv->pages + i, 1);
  2843. obj_priv->page_cpu_valid[i] = 1;
  2844. }
  2845. /* It should now be out of any other write domains, and we can update
  2846. * the domain values for our changes.
  2847. */
  2848. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2849. old_read_domains = obj->read_domains;
  2850. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2851. trace_i915_gem_object_change_domain(obj,
  2852. old_read_domains,
  2853. obj->write_domain);
  2854. return 0;
  2855. }
  2856. static int
  2857. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  2858. struct drm_file *file_priv,
  2859. struct drm_i915_gem_exec_object2 *entry,
  2860. struct drm_i915_gem_relocation_entry *reloc)
  2861. {
  2862. struct drm_device *dev = obj->base.dev;
  2863. struct drm_gem_object *target_obj;
  2864. uint32_t target_offset;
  2865. int ret = -EINVAL;
  2866. target_obj = drm_gem_object_lookup(dev, file_priv,
  2867. reloc->target_handle);
  2868. if (target_obj == NULL)
  2869. return -ENOENT;
  2870. target_offset = to_intel_bo(target_obj)->gtt_offset;
  2871. #if WATCH_RELOC
  2872. DRM_INFO("%s: obj %p offset %08x target %d "
  2873. "read %08x write %08x gtt %08x "
  2874. "presumed %08x delta %08x\n",
  2875. __func__,
  2876. obj,
  2877. (int) reloc->offset,
  2878. (int) reloc->target_handle,
  2879. (int) reloc->read_domains,
  2880. (int) reloc->write_domain,
  2881. (int) target_offset,
  2882. (int) reloc->presumed_offset,
  2883. reloc->delta);
  2884. #endif
  2885. /* The target buffer should have appeared before us in the
  2886. * exec_object list, so it should have a GTT space bound by now.
  2887. */
  2888. if (target_offset == 0) {
  2889. DRM_ERROR("No GTT space found for object %d\n",
  2890. reloc->target_handle);
  2891. goto err;
  2892. }
  2893. /* Validate that the target is in a valid r/w GPU domain */
  2894. if (reloc->write_domain & (reloc->write_domain - 1)) {
  2895. DRM_ERROR("reloc with multiple write domains: "
  2896. "obj %p target %d offset %d "
  2897. "read %08x write %08x",
  2898. obj, reloc->target_handle,
  2899. (int) reloc->offset,
  2900. reloc->read_domains,
  2901. reloc->write_domain);
  2902. goto err;
  2903. }
  2904. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  2905. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  2906. DRM_ERROR("reloc with read/write CPU domains: "
  2907. "obj %p target %d offset %d "
  2908. "read %08x write %08x",
  2909. obj, reloc->target_handle,
  2910. (int) reloc->offset,
  2911. reloc->read_domains,
  2912. reloc->write_domain);
  2913. goto err;
  2914. }
  2915. if (reloc->write_domain && target_obj->pending_write_domain &&
  2916. reloc->write_domain != target_obj->pending_write_domain) {
  2917. DRM_ERROR("Write domain conflict: "
  2918. "obj %p target %d offset %d "
  2919. "new %08x old %08x\n",
  2920. obj, reloc->target_handle,
  2921. (int) reloc->offset,
  2922. reloc->write_domain,
  2923. target_obj->pending_write_domain);
  2924. goto err;
  2925. }
  2926. target_obj->pending_read_domains |= reloc->read_domains;
  2927. target_obj->pending_write_domain |= reloc->write_domain;
  2928. /* If the relocation already has the right value in it, no
  2929. * more work needs to be done.
  2930. */
  2931. if (target_offset == reloc->presumed_offset)
  2932. goto out;
  2933. /* Check that the relocation address is valid... */
  2934. if (reloc->offset > obj->base.size - 4) {
  2935. DRM_ERROR("Relocation beyond object bounds: "
  2936. "obj %p target %d offset %d size %d.\n",
  2937. obj, reloc->target_handle,
  2938. (int) reloc->offset,
  2939. (int) obj->base.size);
  2940. goto err;
  2941. }
  2942. if (reloc->offset & 3) {
  2943. DRM_ERROR("Relocation not 4-byte aligned: "
  2944. "obj %p target %d offset %d.\n",
  2945. obj, reloc->target_handle,
  2946. (int) reloc->offset);
  2947. goto err;
  2948. }
  2949. /* and points to somewhere within the target object. */
  2950. if (reloc->delta >= target_obj->size) {
  2951. DRM_ERROR("Relocation beyond target object bounds: "
  2952. "obj %p target %d delta %d size %d.\n",
  2953. obj, reloc->target_handle,
  2954. (int) reloc->delta,
  2955. (int) target_obj->size);
  2956. goto err;
  2957. }
  2958. reloc->delta += target_offset;
  2959. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  2960. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  2961. char *vaddr;
  2962. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  2963. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  2964. kunmap_atomic(vaddr);
  2965. } else {
  2966. struct drm_i915_private *dev_priv = dev->dev_private;
  2967. uint32_t __iomem *reloc_entry;
  2968. void __iomem *reloc_page;
  2969. ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
  2970. if (ret)
  2971. goto err;
  2972. /* Map the page containing the relocation we're going to perform. */
  2973. reloc->offset += obj->gtt_offset;
  2974. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2975. reloc->offset & PAGE_MASK);
  2976. reloc_entry = (uint32_t __iomem *)
  2977. (reloc_page + (reloc->offset & ~PAGE_MASK));
  2978. iowrite32(reloc->delta, reloc_entry);
  2979. io_mapping_unmap_atomic(reloc_page);
  2980. }
  2981. /* and update the user's relocation entry */
  2982. reloc->presumed_offset = target_offset;
  2983. out:
  2984. ret = 0;
  2985. err:
  2986. drm_gem_object_unreference(target_obj);
  2987. return ret;
  2988. }
  2989. static int
  2990. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  2991. struct drm_file *file_priv,
  2992. struct drm_i915_gem_exec_object2 *entry)
  2993. {
  2994. struct drm_i915_gem_relocation_entry __user *user_relocs;
  2995. int i, ret;
  2996. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  2997. for (i = 0; i < entry->relocation_count; i++) {
  2998. struct drm_i915_gem_relocation_entry reloc;
  2999. if (__copy_from_user_inatomic(&reloc,
  3000. user_relocs+i,
  3001. sizeof(reloc)))
  3002. return -EFAULT;
  3003. ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
  3004. if (ret)
  3005. return ret;
  3006. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  3007. &reloc.presumed_offset,
  3008. sizeof(reloc.presumed_offset)))
  3009. return -EFAULT;
  3010. }
  3011. return 0;
  3012. }
  3013. static int
  3014. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  3015. struct drm_file *file_priv,
  3016. struct drm_i915_gem_exec_object2 *entry,
  3017. struct drm_i915_gem_relocation_entry *relocs)
  3018. {
  3019. int i, ret;
  3020. for (i = 0; i < entry->relocation_count; i++) {
  3021. ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
  3022. if (ret)
  3023. return ret;
  3024. }
  3025. return 0;
  3026. }
  3027. static int
  3028. i915_gem_execbuffer_relocate(struct drm_device *dev,
  3029. struct drm_file *file,
  3030. struct drm_gem_object **object_list,
  3031. struct drm_i915_gem_exec_object2 *exec_list,
  3032. int count)
  3033. {
  3034. int i, ret;
  3035. for (i = 0; i < count; i++) {
  3036. struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
  3037. obj->base.pending_read_domains = 0;
  3038. obj->base.pending_write_domain = 0;
  3039. ret = i915_gem_execbuffer_relocate_object(obj, file,
  3040. &exec_list[i]);
  3041. if (ret)
  3042. return ret;
  3043. }
  3044. return 0;
  3045. }
  3046. static int
  3047. i915_gem_execbuffer_reserve(struct drm_device *dev,
  3048. struct drm_file *file,
  3049. struct drm_gem_object **object_list,
  3050. struct drm_i915_gem_exec_object2 *exec_list,
  3051. int count)
  3052. {
  3053. struct drm_i915_private *dev_priv = dev->dev_private;
  3054. int ret, i, retry;
  3055. /* attempt to pin all of the buffers into the GTT */
  3056. retry = 0;
  3057. do {
  3058. ret = 0;
  3059. for (i = 0; i < count; i++) {
  3060. struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
  3061. struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
  3062. bool need_fence =
  3063. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  3064. obj->tiling_mode != I915_TILING_NONE;
  3065. /* g33/pnv can't fence buffers in the unmappable part */
  3066. bool need_mappable =
  3067. entry->relocation_count ? true : need_fence;
  3068. /* Check fence reg constraints and rebind if necessary */
  3069. if (need_mappable && !obj->map_and_fenceable) {
  3070. ret = i915_gem_object_unbind(&obj->base);
  3071. if (ret)
  3072. break;
  3073. }
  3074. ret = i915_gem_object_pin(&obj->base,
  3075. entry->alignment,
  3076. need_mappable);
  3077. if (ret)
  3078. break;
  3079. /*
  3080. * Pre-965 chips need a fence register set up in order
  3081. * to properly handle blits to/from tiled surfaces.
  3082. */
  3083. if (need_fence) {
  3084. ret = i915_gem_object_get_fence_reg(&obj->base, true);
  3085. if (ret) {
  3086. i915_gem_object_unpin(&obj->base);
  3087. break;
  3088. }
  3089. dev_priv->fence_regs[obj->fence_reg].gpu = true;
  3090. }
  3091. entry->offset = obj->gtt_offset;
  3092. }
  3093. while (i--)
  3094. i915_gem_object_unpin(object_list[i]);
  3095. if (ret != -ENOSPC || retry > 1)
  3096. return ret;
  3097. /* First attempt, just clear anything that is purgeable.
  3098. * Second attempt, clear the entire GTT.
  3099. */
  3100. ret = i915_gem_evict_everything(dev, retry == 0);
  3101. if (ret)
  3102. return ret;
  3103. retry++;
  3104. } while (1);
  3105. }
  3106. static int
  3107. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  3108. struct drm_file *file,
  3109. struct drm_gem_object **object_list,
  3110. struct drm_i915_gem_exec_object2 *exec_list,
  3111. int count)
  3112. {
  3113. struct drm_i915_gem_relocation_entry *reloc;
  3114. int i, total, ret;
  3115. for (i = 0; i < count; i++) {
  3116. struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
  3117. obj->in_execbuffer = false;
  3118. }
  3119. mutex_unlock(&dev->struct_mutex);
  3120. total = 0;
  3121. for (i = 0; i < count; i++)
  3122. total += exec_list[i].relocation_count;
  3123. reloc = drm_malloc_ab(total, sizeof(*reloc));
  3124. if (reloc == NULL) {
  3125. mutex_lock(&dev->struct_mutex);
  3126. return -ENOMEM;
  3127. }
  3128. total = 0;
  3129. for (i = 0; i < count; i++) {
  3130. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3131. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3132. if (copy_from_user(reloc+total, user_relocs,
  3133. exec_list[i].relocation_count *
  3134. sizeof(*reloc))) {
  3135. ret = -EFAULT;
  3136. mutex_lock(&dev->struct_mutex);
  3137. goto err;
  3138. }
  3139. total += exec_list[i].relocation_count;
  3140. }
  3141. ret = i915_mutex_lock_interruptible(dev);
  3142. if (ret) {
  3143. mutex_lock(&dev->struct_mutex);
  3144. goto err;
  3145. }
  3146. ret = i915_gem_execbuffer_reserve(dev, file,
  3147. object_list, exec_list,
  3148. count);
  3149. if (ret)
  3150. goto err;
  3151. total = 0;
  3152. for (i = 0; i < count; i++) {
  3153. struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
  3154. obj->base.pending_read_domains = 0;
  3155. obj->base.pending_write_domain = 0;
  3156. ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
  3157. &exec_list[i],
  3158. reloc + total);
  3159. if (ret)
  3160. goto err;
  3161. total += exec_list[i].relocation_count;
  3162. }
  3163. /* Leave the user relocations as are, this is the painfully slow path,
  3164. * and we want to avoid the complication of dropping the lock whilst
  3165. * having buffers reserved in the aperture and so causing spurious
  3166. * ENOSPC for random operations.
  3167. */
  3168. err:
  3169. drm_free_large(reloc);
  3170. return ret;
  3171. }
  3172. static int
  3173. i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
  3174. struct drm_file *file,
  3175. struct intel_ring_buffer *ring,
  3176. struct drm_gem_object **objects,
  3177. int count)
  3178. {
  3179. struct change_domains cd;
  3180. int ret, i;
  3181. cd.invalidate_domains = 0;
  3182. cd.flush_domains = 0;
  3183. cd.flush_rings = 0;
  3184. for (i = 0; i < count; i++)
  3185. i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
  3186. if (cd.invalidate_domains | cd.flush_domains) {
  3187. #if WATCH_EXEC
  3188. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  3189. __func__,
  3190. cd.invalidate_domains,
  3191. cd.flush_domains);
  3192. #endif
  3193. i915_gem_flush(dev, file,
  3194. cd.invalidate_domains,
  3195. cd.flush_domains,
  3196. cd.flush_rings);
  3197. }
  3198. for (i = 0; i < count; i++) {
  3199. struct drm_i915_gem_object *obj = to_intel_bo(objects[i]);
  3200. /* XXX replace with semaphores */
  3201. if (obj->ring && ring != obj->ring) {
  3202. ret = i915_gem_object_wait_rendering(&obj->base, true);
  3203. if (ret)
  3204. return ret;
  3205. }
  3206. }
  3207. return 0;
  3208. }
  3209. /* Throttle our rendering by waiting until the ring has completed our requests
  3210. * emitted over 20 msec ago.
  3211. *
  3212. * Note that if we were to use the current jiffies each time around the loop,
  3213. * we wouldn't escape the function with any frames outstanding if the time to
  3214. * render a frame was over 20ms.
  3215. *
  3216. * This should get us reasonable parallelism between CPU and GPU but also
  3217. * relatively low latency when blocking on a particular request to finish.
  3218. */
  3219. static int
  3220. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3221. {
  3222. struct drm_i915_private *dev_priv = dev->dev_private;
  3223. struct drm_i915_file_private *file_priv = file->driver_priv;
  3224. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3225. struct drm_i915_gem_request *request;
  3226. struct intel_ring_buffer *ring = NULL;
  3227. u32 seqno = 0;
  3228. int ret;
  3229. spin_lock(&file_priv->mm.lock);
  3230. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3231. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3232. break;
  3233. ring = request->ring;
  3234. seqno = request->seqno;
  3235. }
  3236. spin_unlock(&file_priv->mm.lock);
  3237. if (seqno == 0)
  3238. return 0;
  3239. ret = 0;
  3240. if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
  3241. /* And wait for the seqno passing without holding any locks and
  3242. * causing extra latency for others. This is safe as the irq
  3243. * generation is designed to be run atomically and so is
  3244. * lockless.
  3245. */
  3246. ring->user_irq_get(ring);
  3247. ret = wait_event_interruptible(ring->irq_queue,
  3248. i915_seqno_passed(ring->get_seqno(ring), seqno)
  3249. || atomic_read(&dev_priv->mm.wedged));
  3250. ring->user_irq_put(ring);
  3251. if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
  3252. ret = -EIO;
  3253. }
  3254. if (ret == 0)
  3255. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3256. return ret;
  3257. }
  3258. static int
  3259. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
  3260. uint64_t exec_offset)
  3261. {
  3262. uint32_t exec_start, exec_len;
  3263. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  3264. exec_len = (uint32_t) exec->batch_len;
  3265. if ((exec_start | exec_len) & 0x7)
  3266. return -EINVAL;
  3267. if (!exec_start)
  3268. return -EINVAL;
  3269. return 0;
  3270. }
  3271. static int
  3272. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  3273. int count)
  3274. {
  3275. int i;
  3276. for (i = 0; i < count; i++) {
  3277. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  3278. int length; /* limited by fault_in_pages_readable() */
  3279. /* First check for malicious input causing overflow */
  3280. if (exec[i].relocation_count >
  3281. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  3282. return -EINVAL;
  3283. length = exec[i].relocation_count *
  3284. sizeof(struct drm_i915_gem_relocation_entry);
  3285. if (!access_ok(VERIFY_READ, ptr, length))
  3286. return -EFAULT;
  3287. /* we may also need to update the presumed offsets */
  3288. if (!access_ok(VERIFY_WRITE, ptr, length))
  3289. return -EFAULT;
  3290. if (fault_in_pages_readable(ptr, length))
  3291. return -EFAULT;
  3292. }
  3293. return 0;
  3294. }
  3295. static int
  3296. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  3297. struct drm_file *file,
  3298. struct drm_i915_gem_execbuffer2 *args,
  3299. struct drm_i915_gem_exec_object2 *exec_list)
  3300. {
  3301. drm_i915_private_t *dev_priv = dev->dev_private;
  3302. struct drm_gem_object **object_list = NULL;
  3303. struct drm_gem_object *batch_obj;
  3304. struct drm_clip_rect *cliprects = NULL;
  3305. struct drm_i915_gem_request *request = NULL;
  3306. int ret, i, flips;
  3307. uint64_t exec_offset;
  3308. struct intel_ring_buffer *ring = NULL;
  3309. ret = i915_gem_check_is_wedged(dev);
  3310. if (ret)
  3311. return ret;
  3312. ret = validate_exec_list(exec_list, args->buffer_count);
  3313. if (ret)
  3314. return ret;
  3315. #if WATCH_EXEC
  3316. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3317. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3318. #endif
  3319. switch (args->flags & I915_EXEC_RING_MASK) {
  3320. case I915_EXEC_DEFAULT:
  3321. case I915_EXEC_RENDER:
  3322. ring = &dev_priv->render_ring;
  3323. break;
  3324. case I915_EXEC_BSD:
  3325. if (!HAS_BSD(dev)) {
  3326. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  3327. return -EINVAL;
  3328. }
  3329. ring = &dev_priv->bsd_ring;
  3330. break;
  3331. case I915_EXEC_BLT:
  3332. if (!HAS_BLT(dev)) {
  3333. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  3334. return -EINVAL;
  3335. }
  3336. ring = &dev_priv->blt_ring;
  3337. break;
  3338. default:
  3339. DRM_ERROR("execbuf with unknown ring: %d\n",
  3340. (int)(args->flags & I915_EXEC_RING_MASK));
  3341. return -EINVAL;
  3342. }
  3343. if (args->buffer_count < 1) {
  3344. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3345. return -EINVAL;
  3346. }
  3347. object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
  3348. if (object_list == NULL) {
  3349. DRM_ERROR("Failed to allocate object list for %d buffers\n",
  3350. args->buffer_count);
  3351. ret = -ENOMEM;
  3352. goto pre_mutex_err;
  3353. }
  3354. if (args->num_cliprects != 0) {
  3355. cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
  3356. GFP_KERNEL);
  3357. if (cliprects == NULL) {
  3358. ret = -ENOMEM;
  3359. goto pre_mutex_err;
  3360. }
  3361. ret = copy_from_user(cliprects,
  3362. (struct drm_clip_rect __user *)
  3363. (uintptr_t) args->cliprects_ptr,
  3364. sizeof(*cliprects) * args->num_cliprects);
  3365. if (ret != 0) {
  3366. DRM_ERROR("copy %d cliprects failed: %d\n",
  3367. args->num_cliprects, ret);
  3368. ret = -EFAULT;
  3369. goto pre_mutex_err;
  3370. }
  3371. }
  3372. request = kzalloc(sizeof(*request), GFP_KERNEL);
  3373. if (request == NULL) {
  3374. ret = -ENOMEM;
  3375. goto pre_mutex_err;
  3376. }
  3377. ret = i915_mutex_lock_interruptible(dev);
  3378. if (ret)
  3379. goto pre_mutex_err;
  3380. if (dev_priv->mm.suspended) {
  3381. mutex_unlock(&dev->struct_mutex);
  3382. ret = -EBUSY;
  3383. goto pre_mutex_err;
  3384. }
  3385. /* Look up object handles */
  3386. for (i = 0; i < args->buffer_count; i++) {
  3387. struct drm_i915_gem_object *obj_priv;
  3388. object_list[i] = drm_gem_object_lookup(dev, file,
  3389. exec_list[i].handle);
  3390. if (object_list[i] == NULL) {
  3391. DRM_ERROR("Invalid object handle %d at index %d\n",
  3392. exec_list[i].handle, i);
  3393. /* prevent error path from reading uninitialized data */
  3394. args->buffer_count = i + 1;
  3395. ret = -ENOENT;
  3396. goto err;
  3397. }
  3398. obj_priv = to_intel_bo(object_list[i]);
  3399. if (obj_priv->in_execbuffer) {
  3400. DRM_ERROR("Object %p appears more than once in object list\n",
  3401. object_list[i]);
  3402. /* prevent error path from reading uninitialized data */
  3403. args->buffer_count = i + 1;
  3404. ret = -EINVAL;
  3405. goto err;
  3406. }
  3407. obj_priv->in_execbuffer = true;
  3408. }
  3409. /* Move the objects en-masse into the GTT, evicting if necessary. */
  3410. ret = i915_gem_execbuffer_reserve(dev, file,
  3411. object_list, exec_list,
  3412. args->buffer_count);
  3413. if (ret)
  3414. goto err;
  3415. /* The objects are in their final locations, apply the relocations. */
  3416. ret = i915_gem_execbuffer_relocate(dev, file,
  3417. object_list, exec_list,
  3418. args->buffer_count);
  3419. if (ret) {
  3420. if (ret == -EFAULT) {
  3421. ret = i915_gem_execbuffer_relocate_slow(dev, file,
  3422. object_list,
  3423. exec_list,
  3424. args->buffer_count);
  3425. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  3426. }
  3427. if (ret)
  3428. goto err;
  3429. }
  3430. /* Set the pending read domains for the batch buffer to COMMAND */
  3431. batch_obj = object_list[args->buffer_count-1];
  3432. if (batch_obj->pending_write_domain) {
  3433. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  3434. ret = -EINVAL;
  3435. goto err;
  3436. }
  3437. batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  3438. /* Sanity check the batch buffer */
  3439. exec_offset = to_intel_bo(batch_obj)->gtt_offset;
  3440. ret = i915_gem_check_execbuffer(args, exec_offset);
  3441. if (ret != 0) {
  3442. DRM_ERROR("execbuf with invalid offset/length\n");
  3443. goto err;
  3444. }
  3445. ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
  3446. object_list, args->buffer_count);
  3447. if (ret)
  3448. goto err;
  3449. #if WATCH_COHERENCY
  3450. for (i = 0; i < args->buffer_count; i++) {
  3451. i915_gem_object_check_coherency(object_list[i],
  3452. exec_list[i].handle);
  3453. }
  3454. #endif
  3455. #if WATCH_EXEC
  3456. i915_gem_dump_object(batch_obj,
  3457. args->batch_len,
  3458. __func__,
  3459. ~0);
  3460. #endif
  3461. /* Check for any pending flips. As we only maintain a flip queue depth
  3462. * of 1, we can simply insert a WAIT for the next display flip prior
  3463. * to executing the batch and avoid stalling the CPU.
  3464. */
  3465. flips = 0;
  3466. for (i = 0; i < args->buffer_count; i++) {
  3467. if (object_list[i]->write_domain)
  3468. flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
  3469. }
  3470. if (flips) {
  3471. int plane, flip_mask;
  3472. for (plane = 0; flips >> plane; plane++) {
  3473. if (((flips >> plane) & 1) == 0)
  3474. continue;
  3475. if (plane)
  3476. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  3477. else
  3478. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  3479. ret = intel_ring_begin(ring, 2);
  3480. if (ret)
  3481. goto err;
  3482. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  3483. intel_ring_emit(ring, MI_NOOP);
  3484. intel_ring_advance(ring);
  3485. }
  3486. }
  3487. /* Exec the batchbuffer */
  3488. ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
  3489. if (ret) {
  3490. DRM_ERROR("dispatch failed %d\n", ret);
  3491. goto err;
  3492. }
  3493. for (i = 0; i < args->buffer_count; i++) {
  3494. struct drm_gem_object *obj = object_list[i];
  3495. obj->read_domains = obj->pending_read_domains;
  3496. obj->write_domain = obj->pending_write_domain;
  3497. i915_gem_object_move_to_active(obj, ring);
  3498. if (obj->write_domain) {
  3499. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3500. obj_priv->dirty = 1;
  3501. list_move_tail(&obj_priv->gpu_write_list,
  3502. &ring->gpu_write_list);
  3503. intel_mark_busy(dev, obj);
  3504. }
  3505. trace_i915_gem_object_change_domain(obj,
  3506. obj->read_domains,
  3507. obj->write_domain);
  3508. }
  3509. /*
  3510. * Ensure that the commands in the batch buffer are
  3511. * finished before the interrupt fires
  3512. */
  3513. i915_retire_commands(dev, ring);
  3514. if (i915_add_request(dev, file, request, ring))
  3515. i915_gem_next_request_seqno(dev, ring);
  3516. else
  3517. request = NULL;
  3518. err:
  3519. for (i = 0; i < args->buffer_count; i++) {
  3520. if (object_list[i] == NULL)
  3521. break;
  3522. to_intel_bo(object_list[i])->in_execbuffer = false;
  3523. drm_gem_object_unreference(object_list[i]);
  3524. }
  3525. mutex_unlock(&dev->struct_mutex);
  3526. pre_mutex_err:
  3527. drm_free_large(object_list);
  3528. kfree(cliprects);
  3529. kfree(request);
  3530. return ret;
  3531. }
  3532. /*
  3533. * Legacy execbuffer just creates an exec2 list from the original exec object
  3534. * list array and passes it to the real function.
  3535. */
  3536. int
  3537. i915_gem_execbuffer(struct drm_device *dev, void *data,
  3538. struct drm_file *file_priv)
  3539. {
  3540. struct drm_i915_gem_execbuffer *args = data;
  3541. struct drm_i915_gem_execbuffer2 exec2;
  3542. struct drm_i915_gem_exec_object *exec_list = NULL;
  3543. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3544. int ret, i;
  3545. #if WATCH_EXEC
  3546. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3547. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3548. #endif
  3549. if (args->buffer_count < 1) {
  3550. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3551. return -EINVAL;
  3552. }
  3553. /* Copy in the exec list from userland */
  3554. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  3555. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3556. if (exec_list == NULL || exec2_list == NULL) {
  3557. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3558. args->buffer_count);
  3559. drm_free_large(exec_list);
  3560. drm_free_large(exec2_list);
  3561. return -ENOMEM;
  3562. }
  3563. ret = copy_from_user(exec_list,
  3564. (struct drm_i915_relocation_entry __user *)
  3565. (uintptr_t) args->buffers_ptr,
  3566. sizeof(*exec_list) * args->buffer_count);
  3567. if (ret != 0) {
  3568. DRM_ERROR("copy %d exec entries failed %d\n",
  3569. args->buffer_count, ret);
  3570. drm_free_large(exec_list);
  3571. drm_free_large(exec2_list);
  3572. return -EFAULT;
  3573. }
  3574. for (i = 0; i < args->buffer_count; i++) {
  3575. exec2_list[i].handle = exec_list[i].handle;
  3576. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  3577. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  3578. exec2_list[i].alignment = exec_list[i].alignment;
  3579. exec2_list[i].offset = exec_list[i].offset;
  3580. if (INTEL_INFO(dev)->gen < 4)
  3581. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  3582. else
  3583. exec2_list[i].flags = 0;
  3584. }
  3585. exec2.buffers_ptr = args->buffers_ptr;
  3586. exec2.buffer_count = args->buffer_count;
  3587. exec2.batch_start_offset = args->batch_start_offset;
  3588. exec2.batch_len = args->batch_len;
  3589. exec2.DR1 = args->DR1;
  3590. exec2.DR4 = args->DR4;
  3591. exec2.num_cliprects = args->num_cliprects;
  3592. exec2.cliprects_ptr = args->cliprects_ptr;
  3593. exec2.flags = I915_EXEC_RENDER;
  3594. ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
  3595. if (!ret) {
  3596. /* Copy the new buffer offsets back to the user's exec list. */
  3597. for (i = 0; i < args->buffer_count; i++)
  3598. exec_list[i].offset = exec2_list[i].offset;
  3599. /* ... and back out to userspace */
  3600. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3601. (uintptr_t) args->buffers_ptr,
  3602. exec_list,
  3603. sizeof(*exec_list) * args->buffer_count);
  3604. if (ret) {
  3605. ret = -EFAULT;
  3606. DRM_ERROR("failed to copy %d exec entries "
  3607. "back to user (%d)\n",
  3608. args->buffer_count, ret);
  3609. }
  3610. }
  3611. drm_free_large(exec_list);
  3612. drm_free_large(exec2_list);
  3613. return ret;
  3614. }
  3615. int
  3616. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  3617. struct drm_file *file_priv)
  3618. {
  3619. struct drm_i915_gem_execbuffer2 *args = data;
  3620. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3621. int ret;
  3622. #if WATCH_EXEC
  3623. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3624. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3625. #endif
  3626. if (args->buffer_count < 1) {
  3627. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  3628. return -EINVAL;
  3629. }
  3630. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3631. if (exec2_list == NULL) {
  3632. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3633. args->buffer_count);
  3634. return -ENOMEM;
  3635. }
  3636. ret = copy_from_user(exec2_list,
  3637. (struct drm_i915_relocation_entry __user *)
  3638. (uintptr_t) args->buffers_ptr,
  3639. sizeof(*exec2_list) * args->buffer_count);
  3640. if (ret != 0) {
  3641. DRM_ERROR("copy %d exec entries failed %d\n",
  3642. args->buffer_count, ret);
  3643. drm_free_large(exec2_list);
  3644. return -EFAULT;
  3645. }
  3646. ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
  3647. if (!ret) {
  3648. /* Copy the new buffer offsets back to the user's exec list. */
  3649. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3650. (uintptr_t) args->buffers_ptr,
  3651. exec2_list,
  3652. sizeof(*exec2_list) * args->buffer_count);
  3653. if (ret) {
  3654. ret = -EFAULT;
  3655. DRM_ERROR("failed to copy %d exec entries "
  3656. "back to user (%d)\n",
  3657. args->buffer_count, ret);
  3658. }
  3659. }
  3660. drm_free_large(exec2_list);
  3661. return ret;
  3662. }
  3663. int
  3664. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
  3665. bool map_and_fenceable)
  3666. {
  3667. struct drm_device *dev = obj->dev;
  3668. struct drm_i915_private *dev_priv = dev->dev_private;
  3669. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3670. int ret;
  3671. BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  3672. BUG_ON(map_and_fenceable && !map_and_fenceable);
  3673. WARN_ON(i915_verify_lists(dev));
  3674. if (obj_priv->gtt_space != NULL) {
  3675. if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
  3676. (map_and_fenceable && !obj_priv->map_and_fenceable)) {
  3677. WARN(obj_priv->pin_count,
  3678. "bo is already pinned with incorrect alignment:"
  3679. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  3680. " obj->map_and_fenceable=%d\n",
  3681. obj_priv->gtt_offset, alignment,
  3682. map_and_fenceable,
  3683. obj_priv->map_and_fenceable);
  3684. ret = i915_gem_object_unbind(obj);
  3685. if (ret)
  3686. return ret;
  3687. }
  3688. }
  3689. if (obj_priv->gtt_space == NULL) {
  3690. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  3691. map_and_fenceable);
  3692. if (ret)
  3693. return ret;
  3694. }
  3695. if (obj_priv->pin_count++ == 0) {
  3696. i915_gem_info_add_pin(dev_priv, obj_priv, map_and_fenceable);
  3697. if (!obj_priv->active)
  3698. list_move_tail(&obj_priv->mm_list,
  3699. &dev_priv->mm.pinned_list);
  3700. }
  3701. BUG_ON(!obj_priv->pin_mappable && map_and_fenceable);
  3702. WARN_ON(i915_verify_lists(dev));
  3703. return 0;
  3704. }
  3705. void
  3706. i915_gem_object_unpin(struct drm_gem_object *obj)
  3707. {
  3708. struct drm_device *dev = obj->dev;
  3709. drm_i915_private_t *dev_priv = dev->dev_private;
  3710. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3711. WARN_ON(i915_verify_lists(dev));
  3712. BUG_ON(obj_priv->pin_count == 0);
  3713. BUG_ON(obj_priv->gtt_space == NULL);
  3714. if (--obj_priv->pin_count == 0) {
  3715. if (!obj_priv->active)
  3716. list_move_tail(&obj_priv->mm_list,
  3717. &dev_priv->mm.inactive_list);
  3718. i915_gem_info_remove_pin(dev_priv, obj_priv);
  3719. }
  3720. WARN_ON(i915_verify_lists(dev));
  3721. }
  3722. int
  3723. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3724. struct drm_file *file_priv)
  3725. {
  3726. struct drm_i915_gem_pin *args = data;
  3727. struct drm_gem_object *obj;
  3728. struct drm_i915_gem_object *obj_priv;
  3729. int ret;
  3730. ret = i915_mutex_lock_interruptible(dev);
  3731. if (ret)
  3732. return ret;
  3733. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3734. if (obj == NULL) {
  3735. ret = -ENOENT;
  3736. goto unlock;
  3737. }
  3738. obj_priv = to_intel_bo(obj);
  3739. if (obj_priv->madv != I915_MADV_WILLNEED) {
  3740. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3741. ret = -EINVAL;
  3742. goto out;
  3743. }
  3744. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  3745. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3746. args->handle);
  3747. ret = -EINVAL;
  3748. goto out;
  3749. }
  3750. obj_priv->user_pin_count++;
  3751. obj_priv->pin_filp = file_priv;
  3752. if (obj_priv->user_pin_count == 1) {
  3753. ret = i915_gem_object_pin(obj, args->alignment, true);
  3754. if (ret)
  3755. goto out;
  3756. }
  3757. /* XXX - flush the CPU caches for pinned objects
  3758. * as the X server doesn't manage domains yet
  3759. */
  3760. i915_gem_object_flush_cpu_write_domain(obj);
  3761. args->offset = obj_priv->gtt_offset;
  3762. out:
  3763. drm_gem_object_unreference(obj);
  3764. unlock:
  3765. mutex_unlock(&dev->struct_mutex);
  3766. return ret;
  3767. }
  3768. int
  3769. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3770. struct drm_file *file_priv)
  3771. {
  3772. struct drm_i915_gem_pin *args = data;
  3773. struct drm_gem_object *obj;
  3774. struct drm_i915_gem_object *obj_priv;
  3775. int ret;
  3776. ret = i915_mutex_lock_interruptible(dev);
  3777. if (ret)
  3778. return ret;
  3779. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3780. if (obj == NULL) {
  3781. ret = -ENOENT;
  3782. goto unlock;
  3783. }
  3784. obj_priv = to_intel_bo(obj);
  3785. if (obj_priv->pin_filp != file_priv) {
  3786. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3787. args->handle);
  3788. ret = -EINVAL;
  3789. goto out;
  3790. }
  3791. obj_priv->user_pin_count--;
  3792. if (obj_priv->user_pin_count == 0) {
  3793. obj_priv->pin_filp = NULL;
  3794. i915_gem_object_unpin(obj);
  3795. }
  3796. out:
  3797. drm_gem_object_unreference(obj);
  3798. unlock:
  3799. mutex_unlock(&dev->struct_mutex);
  3800. return ret;
  3801. }
  3802. int
  3803. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3804. struct drm_file *file_priv)
  3805. {
  3806. struct drm_i915_gem_busy *args = data;
  3807. struct drm_gem_object *obj;
  3808. struct drm_i915_gem_object *obj_priv;
  3809. int ret;
  3810. ret = i915_mutex_lock_interruptible(dev);
  3811. if (ret)
  3812. return ret;
  3813. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3814. if (obj == NULL) {
  3815. ret = -ENOENT;
  3816. goto unlock;
  3817. }
  3818. obj_priv = to_intel_bo(obj);
  3819. /* Count all active objects as busy, even if they are currently not used
  3820. * by the gpu. Users of this interface expect objects to eventually
  3821. * become non-busy without any further actions, therefore emit any
  3822. * necessary flushes here.
  3823. */
  3824. args->busy = obj_priv->active;
  3825. if (args->busy) {
  3826. /* Unconditionally flush objects, even when the gpu still uses this
  3827. * object. Userspace calling this function indicates that it wants to
  3828. * use this buffer rather sooner than later, so issuing the required
  3829. * flush earlier is beneficial.
  3830. */
  3831. if (obj->write_domain & I915_GEM_GPU_DOMAINS)
  3832. i915_gem_flush_ring(dev, file_priv,
  3833. obj_priv->ring,
  3834. 0, obj->write_domain);
  3835. /* Update the active list for the hardware's current position.
  3836. * Otherwise this only updates on a delayed timer or when irqs
  3837. * are actually unmasked, and our working set ends up being
  3838. * larger than required.
  3839. */
  3840. i915_gem_retire_requests_ring(dev, obj_priv->ring);
  3841. args->busy = obj_priv->active;
  3842. }
  3843. drm_gem_object_unreference(obj);
  3844. unlock:
  3845. mutex_unlock(&dev->struct_mutex);
  3846. return ret;
  3847. }
  3848. int
  3849. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3850. struct drm_file *file_priv)
  3851. {
  3852. return i915_gem_ring_throttle(dev, file_priv);
  3853. }
  3854. int
  3855. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3856. struct drm_file *file_priv)
  3857. {
  3858. struct drm_i915_gem_madvise *args = data;
  3859. struct drm_gem_object *obj;
  3860. struct drm_i915_gem_object *obj_priv;
  3861. int ret;
  3862. switch (args->madv) {
  3863. case I915_MADV_DONTNEED:
  3864. case I915_MADV_WILLNEED:
  3865. break;
  3866. default:
  3867. return -EINVAL;
  3868. }
  3869. ret = i915_mutex_lock_interruptible(dev);
  3870. if (ret)
  3871. return ret;
  3872. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3873. if (obj == NULL) {
  3874. ret = -ENOENT;
  3875. goto unlock;
  3876. }
  3877. obj_priv = to_intel_bo(obj);
  3878. if (obj_priv->pin_count) {
  3879. ret = -EINVAL;
  3880. goto out;
  3881. }
  3882. if (obj_priv->madv != __I915_MADV_PURGED)
  3883. obj_priv->madv = args->madv;
  3884. /* if the object is no longer bound, discard its backing storage */
  3885. if (i915_gem_object_is_purgeable(obj_priv) &&
  3886. obj_priv->gtt_space == NULL)
  3887. i915_gem_object_truncate(obj);
  3888. args->retained = obj_priv->madv != __I915_MADV_PURGED;
  3889. out:
  3890. drm_gem_object_unreference(obj);
  3891. unlock:
  3892. mutex_unlock(&dev->struct_mutex);
  3893. return ret;
  3894. }
  3895. struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
  3896. size_t size)
  3897. {
  3898. struct drm_i915_private *dev_priv = dev->dev_private;
  3899. struct drm_i915_gem_object *obj;
  3900. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3901. if (obj == NULL)
  3902. return NULL;
  3903. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3904. kfree(obj);
  3905. return NULL;
  3906. }
  3907. i915_gem_info_add_obj(dev_priv, size);
  3908. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3909. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3910. obj->agp_type = AGP_USER_MEMORY;
  3911. obj->base.driver_private = NULL;
  3912. obj->fence_reg = I915_FENCE_REG_NONE;
  3913. INIT_LIST_HEAD(&obj->mm_list);
  3914. INIT_LIST_HEAD(&obj->ring_list);
  3915. INIT_LIST_HEAD(&obj->gpu_write_list);
  3916. obj->madv = I915_MADV_WILLNEED;
  3917. /* Avoid an unnecessary call to unbind on the first bind. */
  3918. obj->map_and_fenceable = true;
  3919. return &obj->base;
  3920. }
  3921. int i915_gem_init_object(struct drm_gem_object *obj)
  3922. {
  3923. BUG();
  3924. return 0;
  3925. }
  3926. static void i915_gem_free_object_tail(struct drm_gem_object *obj)
  3927. {
  3928. struct drm_device *dev = obj->dev;
  3929. drm_i915_private_t *dev_priv = dev->dev_private;
  3930. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3931. int ret;
  3932. ret = i915_gem_object_unbind(obj);
  3933. if (ret == -ERESTARTSYS) {
  3934. list_move(&obj_priv->mm_list,
  3935. &dev_priv->mm.deferred_free_list);
  3936. return;
  3937. }
  3938. if (obj->map_list.map)
  3939. i915_gem_free_mmap_offset(obj);
  3940. drm_gem_object_release(obj);
  3941. i915_gem_info_remove_obj(dev_priv, obj->size);
  3942. kfree(obj_priv->page_cpu_valid);
  3943. kfree(obj_priv->bit_17);
  3944. kfree(obj_priv);
  3945. }
  3946. void i915_gem_free_object(struct drm_gem_object *obj)
  3947. {
  3948. struct drm_device *dev = obj->dev;
  3949. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3950. trace_i915_gem_object_destroy(obj);
  3951. while (obj_priv->pin_count > 0)
  3952. i915_gem_object_unpin(obj);
  3953. if (obj_priv->phys_obj)
  3954. i915_gem_detach_phys_object(dev, obj);
  3955. i915_gem_free_object_tail(obj);
  3956. }
  3957. int
  3958. i915_gem_idle(struct drm_device *dev)
  3959. {
  3960. drm_i915_private_t *dev_priv = dev->dev_private;
  3961. int ret;
  3962. mutex_lock(&dev->struct_mutex);
  3963. if (dev_priv->mm.suspended) {
  3964. mutex_unlock(&dev->struct_mutex);
  3965. return 0;
  3966. }
  3967. ret = i915_gpu_idle(dev);
  3968. if (ret) {
  3969. mutex_unlock(&dev->struct_mutex);
  3970. return ret;
  3971. }
  3972. /* Under UMS, be paranoid and evict. */
  3973. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3974. ret = i915_gem_evict_inactive(dev, false);
  3975. if (ret) {
  3976. mutex_unlock(&dev->struct_mutex);
  3977. return ret;
  3978. }
  3979. }
  3980. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3981. * We need to replace this with a semaphore, or something.
  3982. * And not confound mm.suspended!
  3983. */
  3984. dev_priv->mm.suspended = 1;
  3985. del_timer_sync(&dev_priv->hangcheck_timer);
  3986. i915_kernel_lost_context(dev);
  3987. i915_gem_cleanup_ringbuffer(dev);
  3988. mutex_unlock(&dev->struct_mutex);
  3989. /* Cancel the retire work handler, which should be idle now. */
  3990. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3991. return 0;
  3992. }
  3993. /*
  3994. * 965+ support PIPE_CONTROL commands, which provide finer grained control
  3995. * over cache flushing.
  3996. */
  3997. static int
  3998. i915_gem_init_pipe_control(struct drm_device *dev)
  3999. {
  4000. drm_i915_private_t *dev_priv = dev->dev_private;
  4001. struct drm_gem_object *obj;
  4002. struct drm_i915_gem_object *obj_priv;
  4003. int ret;
  4004. obj = i915_gem_alloc_object(dev, 4096);
  4005. if (obj == NULL) {
  4006. DRM_ERROR("Failed to allocate seqno page\n");
  4007. ret = -ENOMEM;
  4008. goto err;
  4009. }
  4010. obj_priv = to_intel_bo(obj);
  4011. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  4012. ret = i915_gem_object_pin(obj, 4096, true);
  4013. if (ret)
  4014. goto err_unref;
  4015. dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
  4016. dev_priv->seqno_page = kmap(obj_priv->pages[0]);
  4017. if (dev_priv->seqno_page == NULL)
  4018. goto err_unpin;
  4019. dev_priv->seqno_obj = obj;
  4020. memset(dev_priv->seqno_page, 0, PAGE_SIZE);
  4021. return 0;
  4022. err_unpin:
  4023. i915_gem_object_unpin(obj);
  4024. err_unref:
  4025. drm_gem_object_unreference(obj);
  4026. err:
  4027. return ret;
  4028. }
  4029. static void
  4030. i915_gem_cleanup_pipe_control(struct drm_device *dev)
  4031. {
  4032. drm_i915_private_t *dev_priv = dev->dev_private;
  4033. struct drm_gem_object *obj;
  4034. struct drm_i915_gem_object *obj_priv;
  4035. obj = dev_priv->seqno_obj;
  4036. obj_priv = to_intel_bo(obj);
  4037. kunmap(obj_priv->pages[0]);
  4038. i915_gem_object_unpin(obj);
  4039. drm_gem_object_unreference(obj);
  4040. dev_priv->seqno_obj = NULL;
  4041. dev_priv->seqno_page = NULL;
  4042. }
  4043. int
  4044. i915_gem_init_ringbuffer(struct drm_device *dev)
  4045. {
  4046. drm_i915_private_t *dev_priv = dev->dev_private;
  4047. int ret;
  4048. if (HAS_PIPE_CONTROL(dev)) {
  4049. ret = i915_gem_init_pipe_control(dev);
  4050. if (ret)
  4051. return ret;
  4052. }
  4053. ret = intel_init_render_ring_buffer(dev);
  4054. if (ret)
  4055. goto cleanup_pipe_control;
  4056. if (HAS_BSD(dev)) {
  4057. ret = intel_init_bsd_ring_buffer(dev);
  4058. if (ret)
  4059. goto cleanup_render_ring;
  4060. }
  4061. if (HAS_BLT(dev)) {
  4062. ret = intel_init_blt_ring_buffer(dev);
  4063. if (ret)
  4064. goto cleanup_bsd_ring;
  4065. }
  4066. dev_priv->next_seqno = 1;
  4067. return 0;
  4068. cleanup_bsd_ring:
  4069. intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
  4070. cleanup_render_ring:
  4071. intel_cleanup_ring_buffer(&dev_priv->render_ring);
  4072. cleanup_pipe_control:
  4073. if (HAS_PIPE_CONTROL(dev))
  4074. i915_gem_cleanup_pipe_control(dev);
  4075. return ret;
  4076. }
  4077. void
  4078. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4079. {
  4080. drm_i915_private_t *dev_priv = dev->dev_private;
  4081. intel_cleanup_ring_buffer(&dev_priv->render_ring);
  4082. intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
  4083. intel_cleanup_ring_buffer(&dev_priv->blt_ring);
  4084. if (HAS_PIPE_CONTROL(dev))
  4085. i915_gem_cleanup_pipe_control(dev);
  4086. }
  4087. int
  4088. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  4089. struct drm_file *file_priv)
  4090. {
  4091. drm_i915_private_t *dev_priv = dev->dev_private;
  4092. int ret;
  4093. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4094. return 0;
  4095. if (atomic_read(&dev_priv->mm.wedged)) {
  4096. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  4097. atomic_set(&dev_priv->mm.wedged, 0);
  4098. }
  4099. mutex_lock(&dev->struct_mutex);
  4100. dev_priv->mm.suspended = 0;
  4101. ret = i915_gem_init_ringbuffer(dev);
  4102. if (ret != 0) {
  4103. mutex_unlock(&dev->struct_mutex);
  4104. return ret;
  4105. }
  4106. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  4107. BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
  4108. BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
  4109. BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
  4110. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  4111. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  4112. BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
  4113. BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
  4114. BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
  4115. mutex_unlock(&dev->struct_mutex);
  4116. ret = drm_irq_install(dev);
  4117. if (ret)
  4118. goto cleanup_ringbuffer;
  4119. return 0;
  4120. cleanup_ringbuffer:
  4121. mutex_lock(&dev->struct_mutex);
  4122. i915_gem_cleanup_ringbuffer(dev);
  4123. dev_priv->mm.suspended = 1;
  4124. mutex_unlock(&dev->struct_mutex);
  4125. return ret;
  4126. }
  4127. int
  4128. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  4129. struct drm_file *file_priv)
  4130. {
  4131. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4132. return 0;
  4133. drm_irq_uninstall(dev);
  4134. return i915_gem_idle(dev);
  4135. }
  4136. void
  4137. i915_gem_lastclose(struct drm_device *dev)
  4138. {
  4139. int ret;
  4140. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4141. return;
  4142. ret = i915_gem_idle(dev);
  4143. if (ret)
  4144. DRM_ERROR("failed to idle hardware: %d\n", ret);
  4145. }
  4146. static void
  4147. init_ring_lists(struct intel_ring_buffer *ring)
  4148. {
  4149. INIT_LIST_HEAD(&ring->active_list);
  4150. INIT_LIST_HEAD(&ring->request_list);
  4151. INIT_LIST_HEAD(&ring->gpu_write_list);
  4152. }
  4153. void
  4154. i915_gem_load(struct drm_device *dev)
  4155. {
  4156. int i;
  4157. drm_i915_private_t *dev_priv = dev->dev_private;
  4158. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  4159. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  4160. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  4161. INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
  4162. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4163. INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
  4164. init_ring_lists(&dev_priv->render_ring);
  4165. init_ring_lists(&dev_priv->bsd_ring);
  4166. init_ring_lists(&dev_priv->blt_ring);
  4167. for (i = 0; i < 16; i++)
  4168. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4169. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4170. i915_gem_retire_work_handler);
  4171. init_completion(&dev_priv->error_completion);
  4172. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4173. if (IS_GEN3(dev)) {
  4174. u32 tmp = I915_READ(MI_ARB_STATE);
  4175. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  4176. /* arb state is a masked write, so set bit + bit in mask */
  4177. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  4178. I915_WRITE(MI_ARB_STATE, tmp);
  4179. }
  4180. }
  4181. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4182. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4183. dev_priv->fence_reg_start = 3;
  4184. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4185. dev_priv->num_fence_regs = 16;
  4186. else
  4187. dev_priv->num_fence_regs = 8;
  4188. /* Initialize fence registers to zero */
  4189. switch (INTEL_INFO(dev)->gen) {
  4190. case 6:
  4191. for (i = 0; i < 16; i++)
  4192. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
  4193. break;
  4194. case 5:
  4195. case 4:
  4196. for (i = 0; i < 16; i++)
  4197. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  4198. break;
  4199. case 3:
  4200. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4201. for (i = 0; i < 8; i++)
  4202. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  4203. case 2:
  4204. for (i = 0; i < 8; i++)
  4205. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  4206. break;
  4207. }
  4208. i915_gem_detect_bit_6_swizzle(dev);
  4209. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4210. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  4211. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  4212. register_shrinker(&dev_priv->mm.inactive_shrinker);
  4213. }
  4214. /*
  4215. * Create a physically contiguous memory object for this object
  4216. * e.g. for cursor + overlay regs
  4217. */
  4218. static int i915_gem_init_phys_object(struct drm_device *dev,
  4219. int id, int size, int align)
  4220. {
  4221. drm_i915_private_t *dev_priv = dev->dev_private;
  4222. struct drm_i915_gem_phys_object *phys_obj;
  4223. int ret;
  4224. if (dev_priv->mm.phys_objs[id - 1] || !size)
  4225. return 0;
  4226. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  4227. if (!phys_obj)
  4228. return -ENOMEM;
  4229. phys_obj->id = id;
  4230. phys_obj->handle = drm_pci_alloc(dev, size, align);
  4231. if (!phys_obj->handle) {
  4232. ret = -ENOMEM;
  4233. goto kfree_obj;
  4234. }
  4235. #ifdef CONFIG_X86
  4236. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4237. #endif
  4238. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  4239. return 0;
  4240. kfree_obj:
  4241. kfree(phys_obj);
  4242. return ret;
  4243. }
  4244. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  4245. {
  4246. drm_i915_private_t *dev_priv = dev->dev_private;
  4247. struct drm_i915_gem_phys_object *phys_obj;
  4248. if (!dev_priv->mm.phys_objs[id - 1])
  4249. return;
  4250. phys_obj = dev_priv->mm.phys_objs[id - 1];
  4251. if (phys_obj->cur_obj) {
  4252. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  4253. }
  4254. #ifdef CONFIG_X86
  4255. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4256. #endif
  4257. drm_pci_free(dev, phys_obj->handle);
  4258. kfree(phys_obj);
  4259. dev_priv->mm.phys_objs[id - 1] = NULL;
  4260. }
  4261. void i915_gem_free_all_phys_object(struct drm_device *dev)
  4262. {
  4263. int i;
  4264. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  4265. i915_gem_free_phys_object(dev, i);
  4266. }
  4267. void i915_gem_detach_phys_object(struct drm_device *dev,
  4268. struct drm_gem_object *obj)
  4269. {
  4270. struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
  4271. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4272. char *vaddr;
  4273. int i;
  4274. int page_count;
  4275. if (!obj_priv->phys_obj)
  4276. return;
  4277. vaddr = obj_priv->phys_obj->handle->vaddr;
  4278. page_count = obj->size / PAGE_SIZE;
  4279. for (i = 0; i < page_count; i++) {
  4280. struct page *page = read_cache_page_gfp(mapping, i,
  4281. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  4282. if (!IS_ERR(page)) {
  4283. char *dst = kmap_atomic(page);
  4284. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  4285. kunmap_atomic(dst);
  4286. drm_clflush_pages(&page, 1);
  4287. set_page_dirty(page);
  4288. mark_page_accessed(page);
  4289. page_cache_release(page);
  4290. }
  4291. }
  4292. intel_gtt_chipset_flush();
  4293. obj_priv->phys_obj->cur_obj = NULL;
  4294. obj_priv->phys_obj = NULL;
  4295. }
  4296. int
  4297. i915_gem_attach_phys_object(struct drm_device *dev,
  4298. struct drm_gem_object *obj,
  4299. int id,
  4300. int align)
  4301. {
  4302. struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
  4303. drm_i915_private_t *dev_priv = dev->dev_private;
  4304. struct drm_i915_gem_object *obj_priv;
  4305. int ret = 0;
  4306. int page_count;
  4307. int i;
  4308. if (id > I915_MAX_PHYS_OBJECT)
  4309. return -EINVAL;
  4310. obj_priv = to_intel_bo(obj);
  4311. if (obj_priv->phys_obj) {
  4312. if (obj_priv->phys_obj->id == id)
  4313. return 0;
  4314. i915_gem_detach_phys_object(dev, obj);
  4315. }
  4316. /* create a new object */
  4317. if (!dev_priv->mm.phys_objs[id - 1]) {
  4318. ret = i915_gem_init_phys_object(dev, id,
  4319. obj->size, align);
  4320. if (ret) {
  4321. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  4322. return ret;
  4323. }
  4324. }
  4325. /* bind to the object */
  4326. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  4327. obj_priv->phys_obj->cur_obj = obj;
  4328. page_count = obj->size / PAGE_SIZE;
  4329. for (i = 0; i < page_count; i++) {
  4330. struct page *page;
  4331. char *dst, *src;
  4332. page = read_cache_page_gfp(mapping, i,
  4333. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  4334. if (IS_ERR(page))
  4335. return PTR_ERR(page);
  4336. src = kmap_atomic(page);
  4337. dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4338. memcpy(dst, src, PAGE_SIZE);
  4339. kunmap_atomic(src);
  4340. mark_page_accessed(page);
  4341. page_cache_release(page);
  4342. }
  4343. return 0;
  4344. }
  4345. static int
  4346. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  4347. struct drm_i915_gem_pwrite *args,
  4348. struct drm_file *file_priv)
  4349. {
  4350. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4351. void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset;
  4352. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  4353. DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size);
  4354. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  4355. unsigned long unwritten;
  4356. /* The physical object once assigned is fixed for the lifetime
  4357. * of the obj, so we can safely drop the lock and continue
  4358. * to access vaddr.
  4359. */
  4360. mutex_unlock(&dev->struct_mutex);
  4361. unwritten = copy_from_user(vaddr, user_data, args->size);
  4362. mutex_lock(&dev->struct_mutex);
  4363. if (unwritten)
  4364. return -EFAULT;
  4365. }
  4366. intel_gtt_chipset_flush();
  4367. return 0;
  4368. }
  4369. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4370. {
  4371. struct drm_i915_file_private *file_priv = file->driver_priv;
  4372. /* Clean up our request list when the client is going away, so that
  4373. * later retire_requests won't dereference our soon-to-be-gone
  4374. * file_priv.
  4375. */
  4376. spin_lock(&file_priv->mm.lock);
  4377. while (!list_empty(&file_priv->mm.request_list)) {
  4378. struct drm_i915_gem_request *request;
  4379. request = list_first_entry(&file_priv->mm.request_list,
  4380. struct drm_i915_gem_request,
  4381. client_list);
  4382. list_del(&request->client_list);
  4383. request->file_priv = NULL;
  4384. }
  4385. spin_unlock(&file_priv->mm.lock);
  4386. }
  4387. static int
  4388. i915_gpu_is_active(struct drm_device *dev)
  4389. {
  4390. drm_i915_private_t *dev_priv = dev->dev_private;
  4391. int lists_empty;
  4392. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  4393. list_empty(&dev_priv->mm.active_list);
  4394. return !lists_empty;
  4395. }
  4396. static int
  4397. i915_gem_inactive_shrink(struct shrinker *shrinker,
  4398. int nr_to_scan,
  4399. gfp_t gfp_mask)
  4400. {
  4401. struct drm_i915_private *dev_priv =
  4402. container_of(shrinker,
  4403. struct drm_i915_private,
  4404. mm.inactive_shrinker);
  4405. struct drm_device *dev = dev_priv->dev;
  4406. struct drm_i915_gem_object *obj, *next;
  4407. int cnt;
  4408. if (!mutex_trylock(&dev->struct_mutex))
  4409. return 0;
  4410. /* "fast-path" to count number of available objects */
  4411. if (nr_to_scan == 0) {
  4412. cnt = 0;
  4413. list_for_each_entry(obj,
  4414. &dev_priv->mm.inactive_list,
  4415. mm_list)
  4416. cnt++;
  4417. mutex_unlock(&dev->struct_mutex);
  4418. return cnt / 100 * sysctl_vfs_cache_pressure;
  4419. }
  4420. rescan:
  4421. /* first scan for clean buffers */
  4422. i915_gem_retire_requests(dev);
  4423. list_for_each_entry_safe(obj, next,
  4424. &dev_priv->mm.inactive_list,
  4425. mm_list) {
  4426. if (i915_gem_object_is_purgeable(obj)) {
  4427. i915_gem_object_unbind(&obj->base);
  4428. if (--nr_to_scan == 0)
  4429. break;
  4430. }
  4431. }
  4432. /* second pass, evict/count anything still on the inactive list */
  4433. cnt = 0;
  4434. list_for_each_entry_safe(obj, next,
  4435. &dev_priv->mm.inactive_list,
  4436. mm_list) {
  4437. if (nr_to_scan) {
  4438. i915_gem_object_unbind(&obj->base);
  4439. nr_to_scan--;
  4440. } else
  4441. cnt++;
  4442. }
  4443. if (nr_to_scan && i915_gpu_is_active(dev)) {
  4444. /*
  4445. * We are desperate for pages, so as a last resort, wait
  4446. * for the GPU to finish and discard whatever we can.
  4447. * This has a dramatic impact to reduce the number of
  4448. * OOM-killer events whilst running the GPU aggressively.
  4449. */
  4450. if (i915_gpu_idle(dev) == 0)
  4451. goto rescan;
  4452. }
  4453. mutex_unlock(&dev->struct_mutex);
  4454. return cnt / 100 * sysctl_vfs_cache_pressure;
  4455. }