i915_gem.c 135 KB

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