i915_gem.c 131 KB

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