i915_gem.c 138 KB

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