i915_gem.c 129 KB

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