i915_gem.c 131 KB

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