i915_gem.c 134 KB

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