i915_gem.c 133 KB

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