i915_gem.c 140 KB

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