i915_gem.c 136 KB

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